Disable Inavtive Detection (禁止后台检测)

Some website detects user switching to other tabs, which can be disabled by this script. 有些网站会检测用户正处于后台,此脚本可以干掉这些检测。

// ==UserScript==
// @name         Disable Inavtive Detection (禁止后台检测)
// @namespace    https://ipidkun.com/
// @version      20241105.1
// @description  Some website detects user switching to other tabs, which can be disabled by this script. 有些网站会检测用户正处于后台,此脚本可以干掉这些检测。
// @author       ipid
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";

  function pinValue(obj, key, value) {
    try {
      Object.defineProperty(obj, key, {
        enumerable: false,
        configurable: false,

        get: () => {
          return value;
        },

        set: () => {},
      });
    } catch {}
  }

  function pinMethodReturnValue(obj, method, value) {
    try {
      obj[method] = () => value;
    } catch {}
  }

  function disableEvent(eventTarget, name) {
    try {
      eventTarget.addEventListener(
        name,
        (ev) => {
          try {
            ev.preventDefault();
          } catch {}
          try {
            ev.stopImmediatePropagation();
          } catch {}
        },
        { capture: true, passive: false }
      );
    } catch {}
  }

  pinValue(document, "hidden", false);
  pinValue(document, "webkitHidden", false);
  pinValue(document, "visibilityState", "visible");
  pinValue(document, "webkitVisibilityState", "visible");

  disableEvent(window, "blur");
  disableEvent(window, "pagehide");
  disableEvent(document, "visibilitychange");

  pinMethodReturnValue(document, "hasFocus", true);
})();