Greasy Fork

Greasy Fork is available in English.

VK: Check Online

Checks the last online on page user and in dialog

当前为 2022-12-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 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.5.1
// @match           https://vk.com/*
// @connect         vk.com
// @compatible      chrome     Violentmonkey 2.13.3
// @compatible      firefox    Violentmonkey 2.13.3
// @compatible      firefox    Tampermonkey 4.18.1
// @homepageURL     http://greasyfork.icu/en/scripts/403717-vk-check-online
// @supportURL      http://greasyfork.icu/en/scripts/403717-vk-check-online/feedback
// @run-at          document-body
// @noframes
// ==/UserScript==

(function (W) {
  'use strict';

  const IM_ID = 'payload,1,1,0,id';
  const IM_ONLINE = 'payload,1,0,last_seen';
  const PROFILE_ID = 'response,users.get,0,id';
  const PROFILE_ONLINE = 'response,users.get,0,online_info';

  const IM_INFO = {
    can_see: 1,
    platform: 0,
    time: 0,
    mobile: 0,
  };

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

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

  const match = (s) => /online_info|a_start/.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');
    return Date.parse(date) / 1000;
  };

  const request = (url, callback) => {
    const xhr = new XMLHttpRequest();
    xhr.onreadystatechange = () => {
      if (xhr.readyState === xhr.DONE) {
        if (xhr.status === 0 || xhr.status === 200) {
          callback(xhr.responseText);
        }
      }
    };
    xhr.open('GET', url, true);
    xhr.send();
  };

  const foaf = (id) => `https://vk.com/foaf.php?id=${id}`;

  const profile = (data) => {
    const info = getByPath(data, PROFILE_ONLINE);
    if (info && info.visible === true) return Promise.resolve(data);
    const id = getByPath(data, PROFILE_ID);
    if (id === undefined) return Promise.resolve(data);
    return fetch(foaf(id))
      .then((res) => res.text())
      .then(parse)
      .then((time) => {
        setByPath(data, PROFILE_ONLINE, { ...PROFILE_INFO, last_seen: time });
        return data;
      });
  };

  const im = (data, callback) => {
    const info = getByPath(data, IM_ONLINE);
    if (info && info.can_see === 1) return callback(data);
    const id = getByPath(data, IM_ID);
    if (id === undefined) return callback(data);
    request(foaf(id), (text) => {
      const time = parse(text);
      setByPath(data, IM_ONLINE, { ...IM_INFO, time });
      callback(data);
    });
  };

  const proto = W.XMLHttpRequest && W.XMLHttpRequest.prototype;
  proto.send = new Proxy(proto.send, {
    apply(...args) {
      const argumentsList = last(args);
      const self = args[1];
      const body = last(argumentsList);
      if (match(body)) {
        self.onreadystatechange = new Proxy(self.onreadystatechange, {
          apply(...pars) {
            if (self.readyState === proto.DONE) {
              const data = JSON.parse(self.responseText);
              im(data, (patched) => {
                Object.defineProperty(self, 'responseText', {
                  value: JSON.stringify(patched),
                  writable: true,
                });
                Reflect.apply(...pars);
              });
            }
          },
        });
      }
      Reflect.apply(...args);
    },
  });

  W.fetch = new Proxy(W.fetch, {
    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(profile)
        .then((patched) => new Response(JSON.stringify(patched), promise))
        .catch(() => promise);
    },
  });

  W.extend = new Proxy(W.extend, {
    apply(target, self, argumentsList) {
      const object = argumentsList[0];
      const { apiPrefetchCache } = last(argumentsList);
      if (!apiPrefetchCache) return Reflect.apply(target, self, argumentsList);
      const filtered = apiPrefetchCache.filter(({ method }) => method !== 'users.get');
      return Reflect.apply(target, self, [object, { apiPrefetchCache: filtered }]);
    },
  });
})(unsafeWindow || window);