alvkn / Last Online on VK accs (lolz.guru/market)

// ==UserScript==
// @name         Last Online on VK accs (lolz.guru/market)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Showing last seen date on Vk accounts
// @author       alvkN
// @match        lolz.guru/market/vkontakte/*
// @grant        GM_xmlhttpRequest
// @license		 MIT
// ==/UserScript==

function LastSeen(){

    function getLastSeen(id) {
        return new Promise((resolve, reject) => {
            const regex = /<ya:lastLoggedIn dc:date=.*?\/>/;
            const req = new XMLHttpRequest();
            req.open('GET', `https://api.allorigins.win/raw?url=${encodeURIComponent('https://vk.com/foaf.php?id=')}${id}}`, true);
            req.onload = function () {
                let date = new Date(regex.exec(req.responseText)[0].replace("<ya:lastLoggedIn dc:date=\"","").replace("\"/>",""));
                resolve(GetFormatDate(date));
            };
            req.onerror = reject;
            req.send();
        });
    }

    function GetFormatDate(date)
    {
        const days = date.getDate();
        const months = date.getMonth() + 1 < 10 ? "0" + (date.getMonth()+1) : date.getMonth()+1;
        const years = date.getFullYear();
        const hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        const minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        return String(hours+":"+minutes+" "+days+"."+months+"."+years);
    }

    function searchAccs(el) {
        const accsArray = el.querySelectorAll('.marketIndexItem');
        if(!accsArray) return;

        accsArray.forEach((acc) => {
            if(acc.checked) return;
            else acc.checked = true;

            renderLastSeen(acc);
        });
    }

    async function renderLastSeen(acc) {
        const id = await getId(acc);
        const lastseen = await getLastSeen(id);
        const title = acc.getElementsByClassName('marketIndexItem--Title')[0];
        title.textContent = "["+lastseen+"] | "+title.textContent;
    }

    function getId(acc) {
        return new Promise((resolve, reject) => {
            const req = new XMLHttpRequest();
            req.open('GET', `https://lolz.guru/market/${acc.id.replace('marketItem--','')}/`, true);
            req.onerror = reject;
            req.onload = function () {
                const regex = /vk\.com\/id\d+/;
                resolve(regex.exec(req.responseText)[0].replace("vk.com/id",''));
            }
            req.send();
        });
    }

    window.addEventListener('load', () => {
        searchAccs(document.body);

        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                if(mutation.target.nodeType == 1) {
                    searchAccs(mutation.target);
                }
            });
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}

const script = document.createElement('script');
const code = document.createTextNode(`(${LastSeen})();`);

script.appendChild(code);
document.head.appendChild(script);