Sabotage Window/Tab Switch Visibility

Make website completely invisible to the user switching to another window or tab. Warning: may break some websites.

2024-01-28 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

// ==UserScript==
// @name        Sabotage Window/Tab Switch Visibility
// @description Make website completely invisible to the user switching to another window or tab. Warning: may break some websites.
// @author      owowed
// @version     0.0.1
// @namespace   util.owowed.moe
// @license     GPL-3.0-or-later
// @match       *://*/*
// @grant       unsafeWindow
// @run-at      document-start
// @copyright   All rights reserved. Licensed under GPL-3.0-or-later. View license at https://spdx.org/licenses/GPL-3.0-or-later.html
// ==/UserScript==

!function () {
    /* Disable focus and blur event of document and window */

    const windowProto = globalThis.unsafeWindow ?? window;
    
    console.log("Sabotage Window/Tab Switch Visibility is executing...");
    
    function disableFocusBlurEvent(objPrototype) {
        Object.defineProperty(objPrototype, "onfocus", {
            set: () => undefined,
            enumerable: true, configurable: true
        });
        
        Object.defineProperty(objPrototype, "onblur", {
            set: () => undefined,
            enumerable: true, configurable: true
        });

        const oldEventListener = objPrototype.addEventListener;
    
        objPrototype.addEventListener = function (event, ...args) {
            if (event == "blur" || event == "focus") return;
            oldEventListener.call(this, event, ...args);
        }
    }
    
    disableFocusBlurEvent(Document.prototype);
    disableFocusBlurEvent(windowProto);

    /* Disable Page Visibility API */
    
    Object.defineProperty(Document.prototype, "hidden", {
        get: () => false,
        enumerable: true, configurable: true
    });
    
    Object.defineProperty(Document.prototype, "visibilityState", {
        get: () => false,
        enumerable: true, configurable: true
    });
    
    document.addEventListener("visibilitychange", function(e) {
        e.stopImmediatePropagation();
    }, true, true);

    /* Partially disable blur and focus event of element */

    const safeBoxSize = 800;
    const elementOldEventListener = Element.prototype.addEventListener;
    
    Element.prototype.addEventListener = function (event, ...args) {
        if (event == "blur" || event == "focus") {
            const elem = this;
            elementOldEventListener.call(this, event, () => {
                const clientRect = elem.getBoundingClientRect();
    
                if (clientRect.width >= safeBoxSize || clientRect.height >= safeBoxSize) {
                    return;
                }
            });
        }
        elementOldEventListener.call(this, event, ...args);
    };
    
    /* Disable CSS prefers-reduced-motion for JavaScript */
    
    const windowOldMatchMedia = windowProto.matchMedia;
    
    windowProto.matchMedia = function (matchMedia, ...args) {
        if (matchMedia.includes("prefers-reduced-motion") && matchMedia.includes("reduce")) {
            return false;
        }
        return windowOldMatchMedia.call(this, matchMedia, ...args);
    }

    console.log("Sabotage Window/Tab Switch Visibility executed");
}();