Anti Focus Pause / Always Focus

Trick sites into thinking the tab is always focused and visible

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Anti Focus Pause / Always Focus
// @namespace    https://your.custom.namespace
// @version      1.0
// @description  Trick sites into thinking the tab is always focused and visible
// @author       You
// @match        https://viaplay.*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
  'use strict';

  // Override visibility/focus properties
  Object.defineProperty(document, 'hidden', { get: () => false });
  Object.defineProperty(document, 'visibilityState', { get: () => 'visible' });
  document.hasFocus = () => true;

  // Constantly dispatch fake focus events to fool detection
  setInterval(() => {
    window.dispatchEvent(new Event('focus'));
    document.dispatchEvent(new Event('visibilitychange'));
  }, 500);

  // Prevent visibility-related events from triggering logic
  const blockEvent = (e) => {
    e.stopImmediatePropagation();
    e.stopPropagation();
    e.preventDefault();
  };

  ['visibilitychange', 'webkitvisibilitychange', 'blur'].forEach((evt) => {
    window.addEventListener(evt, blockEvent, true);
    document.addEventListener(evt, blockEvent, true);
  });

  // Patch addEventListener to block new focus/blur listeners
  const originalAddEventListener = EventTarget.prototype.addEventListener;
  EventTarget.prototype.addEventListener = function(type, listener, options) {
    if (['visibilitychange', 'webkitvisibilitychange', 'blur', 'focus'].includes(type)) {
      console.log(`Blocked addEventListener for: ${type}`);
      return;
    }
    return originalAddEventListener.call(this, type, listener, options);
  };

  // Re-apply patches in case the site tries to override
  new MutationObserver(() => {
    document.hasFocus = () => true;
    Object.defineProperty(document, 'visibilityState', { get: () => 'visible' });
    Object.defineProperty(document, 'hidden', { get: () => false });
  }).observe(document, { childList: true, subtree: true });

})();