Greasy Fork

Greasy Fork is available in English.

VK: Check Online

Checks the last online on page user and in dialog

当前为 2022-10-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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.2.0
// @match           https://vk.com/*
// @connect         vk.com
// @compatible      chrome     Violentmonkey 2.12.7
// @compatible      firefox    Violentmonkey 2.12.7
// @compatible      firefox    Greasemonkey 4.11
// @homepageURL     http://greasyfork.icu/en/scripts/403717-vk-check-online
// @supportURL      http://greasyfork.icu/en/scripts/403717-vk-check-online/feedback
// @run-at          document-end
// @noframes
// ==/UserScript==

'use strict';

(function (W, D, T) {
  'use strict';

  const ID_PATH = 'response,users.get,0,id';

  const ONLINE_PATH = 'response,users.get,0,online_info';

  const ONLINE_INFO = {
    visible: true,
    last_seen: 0,
    is_online: false,
    is_mobile: false,
  };

  const last = (arr) => arr[arr.length - 1];

  const match = (s) => /online_info/.test(s);

  const getByPath = (data, dataPath) => {
    const path = dataPath.split(',');
    let object = data;
    for (const name of path) {
      const next = object[name];
      if (next === null || next === undefined) return next;
      object = next;
    }
    return object;
  };

  const setByPath = (data, dataPath, value) => {
    const path = dataPath.split(',');
    const len = path.length;
    let obj = data;
    let i = 0;
    let next, prop;
    for (;;) {
      if (typeof obj !== 'object') return false;
      prop = path[i];
      if (i === len - 1) {
        obj[prop] = value;
        return true;
      }
      next = obj[prop];
      if (next === undefined || next === null) {
        next = {};
        obj[prop] = next;
      }
      obj = next;
      i++;
    }
  };

  const parse = (text) => {
    const parser = new DOMParser();
    const body = parser.parseFromString(text, 'text/html');
    const [element] = body.getElementsByTagName('ya:lastloggedin');
    if (!element) return 0;
    const date = element.getAttribute('dc:date');
    const uts = Math.floor(Date.parse(date) / 1000);
    return uts;
  };

  const online = (id) =>
    fetch(`/foaf.php?id=${id}`)
      .then((res) => res.text())
      .then(parse);

  const patch = (data) => {
    const info = getByPath(data, ONLINE_PATH);
    if (info && info.visible === true) return data;
    const id = getByPath(data, ID_PATH);
    return online(id).then((timestamp) => {
      const info = { ...ONLINE_INFO, last_seen: timestamp };
      setByPath(data, ONLINE_PATH, info);
      return data;
    });
  };

  W[T] = new Proxy(W[T], {
    apply(...args) {
      const promise = Reflect.apply(...args);
      const argumentsList = last(args);
      const { body } = last(argumentsList);
      if (!match(body)) return promise;
      return promise
        .then((res) => res.clone())
        .then((res) => res.json())
        .then(patch)
        .then((patched) => new Response(JSON.stringify(patched), promise))
        .catch(() => promise);
    },
  });
})(unsafeWindow || window, document, 'fetch');