Always has focus

Tricks the page into thinking it is always focused

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Always has focus
// @namespace   dev.tock.keepfocus
// @version     1.0
// @description Tricks the page into thinking it is always focused
// @author      Steve Persson
// @match       *://*/*
// @run-at      document-start
// @grant       unsafeWindow
// @license     MIT
// ==/UserScript==

(function() {
    'use strict';

    // Override the document.hasFocus() method to always return true
    document.hasFocus = function () {
        return true;
    };
    
    // Override the document.hidden property and visibilityState
    Object.defineProperty(Document.prototype, "hidden", { get: () => false });
    Object.defineProperty(Document.prototype, "visibilityState", { get: () => "visible" });

    // Override the Page Visibility API properties
    Object.defineProperty(document, "visibilityState", {
        get: function () {
            return "visible";
        }
    });

    Object.defineProperty(document, "hidden", {
        get: function () {
            return false;
        }
    });

    // Stop propagation of visibilitychange and blur events
    window.addEventListener('visibilitychange', function (event) {
        event.stopImmediatePropagation();
    }, true);

    window.addEventListener('blur', function (event) {
        event.stopImmediatePropagation();
    }, true);

    // Some sites might use window.onfocus/onblur
    window.onblur = null;
    window.onfocus = null;
    
    // Override the window.onblur event handler
    if (unsafeWindow) {
        unsafeWindow.onblur = null;
    }

})();