Greasy Fork is available in English.
Checks the last online on page user and in dialog
当前为
/* globals vk, cur, getDateText, GM, GM_xmlhttpRequest */
// ==UserScript==
// @name VK: Check Online
// @name:ru ВК: Проверка онлайна
// @description Checks the last online on page user and in dialog
// @description:ru Проверяет последний онлайн пользователя на странице и в диалогe
// @namespace vk-check-online.user.js
// @license MIT
// @author askornot
// @version 1.0.2
// @match https://vk.com/*
// @connect vk.com
// @compatible chrome Violentmonkey 2.12.7
// @compatible firefox Violentmonkey 2.12.7
// @homepageURL http://greasyfork.icu/en/scripts/403717-vk-check-online
// @supportURL http://greasyfork.icu/en/scripts/403717-vk-check-online/feedback
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// @run-at document-end
// @noframes
// ==/UserScript==
'use strict';
const USERS = new Map();
const request = (id, data = {}) => new Promise((resolve, reject) => {
data.method = 'GET';
data.url = `/foaf.php?id=${id}`;
data.responseType = 'document';
data.onload = resolve;
data.onerror = reject;
typeof !GM && typeof !GM.xmlHttpRequest
? GM.xmlHttpRequest(data)
: GM_xmlhttpRequest(data);
});
const now = () => (Math.floor(Date.now()));
const render = uts => {
const online = document.querySelector('.profile_online_lv') ||
document.querySelector('._im_page_peer_online');
const { lang } = vk;
const text = lang === 3 ? 'last seen' : 'заходил(а)';
online.textContent = `${text} ${getDateText(uts, null)}`;
};
const getTs = rdf => {
const [element] = rdf.getElementsByTagName('ya:lastloggedin');
if (!element) return 0;
const date = element.getAttribute('dc:date');
const uts = Math.floor(Date.parse(date) / 1000); // A getDateText requires unixtime
return uts;
};
const getUser = user => USERS.has(user) ? USERS.get(user) : {};
const start = async () => {
const { options: { user_id: id }, peer } = cur; // VK object
const userId = (id ? id : peer) || 0;
if (userId === 0 || Math.sign(userId) === -1) return;
let { expires = 0, uts } = getUser(userId);
if (expires < now()) {
try {
const { response } = await request(userId);
uts = getTs(response);
USERS.set(userId, {
uts,
expires: now() + 60000 // Every one minute
});
} catch (error) {
uts = 0;
console.error(error.stack);
}
}
if (uts === 0) return;
render(uts);
};
start().catch(console.error);
const filterMutsByClassName = muts => {
return muts.filter((array, i, self) => self.findIndex(_array => (array.target.className === _array.target.className)) === i);
};
new MutationObserver(muts => {
muts = filterMutsByClassName(muts);
for (const { target, addedNodes } of muts) {
const { tagName } = target;
if (tagName === 'DIV' || tagName === 'SPAN') {
const { classList } = target;
if (
(classList.contains('wide_column') && addedNodes.length >= 2) ||
classList.contains('_im_page_peer_online') ||
classList.contains('im-page-chat-contain')
) {
setTimeout(start, 1000);
}
}
}
}).observe(document.getElementById('page_body') || document.body || document, {
childList: true,
subtree: true
});