VK: Check Online

Checks the last online on page user and in dialog

目前为 2020-05-19 提交的版本。查看 最新版本

/* 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.4
// @match           https://vk.com/*
// @connect         vk.com
// @compatible      chrome     Violentmonkey 2.12.7
// @compatible      firefox    Violentmonkey 2.12.7
// @homepageURL     https://gf.qytechs.cn/en/scripts/403717-vk-check-online
// @supportURL      https://gf.qytechs.cn/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
});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址