Multi Tab Visibility

allowing to open many tabs without browser's knowing

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name             Multi Tab Visibility
// @namespace        violentmonkey/tampermonkey script 
// @version          3.0
// @description      allowing to open many tabs without browser's knowing 
// @author           Ojo Ngono
// @include          *://*/*
// @icon             https://i.ibb.co.com/XJSPdz0/large.png
// @exclude          /^(https?:\/\/)([^\/]+\.)?((cloudflare|github|aliyun|reddit|bing|yahoo|microsoft|whatsapp|amazon|ebay|payoneer|paypal|skrill|stripe|stripecdn|tipalti|wise|discord|tokopedia|taobao|taboola|aliexpress|netflix|citigroup|spotify|bankofamerica|hsbc|blogger|(accounts|studio).youtube|atlassian|pinterest|twitter|x|live|linkedin|fastbull|tradingview|deepseek|chatgpt|openai|grok|bilibili|indodax|bmcdn6|fbsbx|googlesyndication|amazon-adsystem|pubmatic|gstatic).com|(greasyfork|openuserjs|telegram|wikipedia|lichess).org|(doubleclick|yahoo).net|proton.me|stripe.network|meta.ai|codepen.io|(shopee|lazada|rakuten|maybank|binance).*|(dana|ovo|bca.co|bri.co|bni.co|bankmandiri.co|desa|(.*).go).id|(.*).(edu|gov))(\/.*)/
// @exclude          /^https?:\/\/(?!(www\.google\.com\/(recaptcha\/|url)|docs\.google\.com\/|drive\.google\.com\/)).*google\..*/
// @exclude          /^https?:\/\/([a-z0-9]+\.)*(facebook|instagram|tiktok)\.com\/(?!(flx\/warn\/|linkshim\/|link\/v2)).*/
// @grant            none
// @license          Copyright OjoNgono
// ==/UserScript==


 (() => {
    'use strict';

    // =========================
    // Visibility
    // =========================

    Object.defineProperty(document, 'hidden', {
        configurable: true,
        get: () => false
    });

    Object.defineProperty(document, 'visibilityState', {
        configurable: true,
        get: () => 'visible'
    });

    document.hasFocus = () => true;

    // =========================
    // Block visibility listeners
    // =========================

    const blockedEvents = new Set([
        'visibilitychange',
        'webkitvisibilitychange',
        'mozvisibilitychange',
        'msvisibilitychange'
    ]);

    const originalAddEventListener =
        EventTarget.prototype.addEventListener;

    EventTarget.prototype.addEventListener =
        function(type, listener, options) {

        if (blockedEvents.has(type)) {

            const wrapped = function(e) {

                Object.defineProperty(document, 'hidden', {
                    configurable: true,
                    get: () => false
                });

                Object.defineProperty(document, 'visibilityState', {
                    configurable: true,
                    get: () => 'visible'
                });

                return listener.call(this, e);
            };

            return originalAddEventListener.call(
                this,
                type,
                wrapped,
                options
            );
        }

        return originalAddEventListener.call(
            this,
            type,
            listener,
            options
        );
    };

    // =========================
    // IntersectionObserver
    // =========================

    if (window.IntersectionObserver) {

        const OriginalIO = window.IntersectionObserver;

        window.IntersectionObserver =
            class extends OriginalIO {

            constructor(callback, options) {

                super((entries, observer) => {

                    entries.forEach(entry => {

                        try {

                            Object.defineProperty(
                                entry,
                                'isIntersecting',
                                {
                                    configurable: true,
                                    get: () => true
                                }
                            );

                            Object.defineProperty(
                                entry,
                                'intersectionRatio',
                                {
                                    configurable: true,
                                    get: () => 1
                                }
                            );

                        } catch(e) {}

                    });

                    callback(entries, observer);

                }, options);
            }
        };
    }

    // =========================
    // elementFromPoint fallback
    // =========================

    const originalElementFromPoint =
        document.elementFromPoint.bind(document);

    document.elementFromPoint = function(x, y) {

        const el = originalElementFromPoint(x, y);

        if (el) return el;

        return document.body;
    };

    // =========================
    // userActivation
    // =========================

    if (navigator.userActivation) {

        try {

            Object.defineProperty(
                navigator.userActivation,
                'isActive',
                {
                    configurable: true,
                    get: () => true
                }
            );

            Object.defineProperty(
                navigator.userActivation,
                'hasBeenActive',
                {
                    configurable: true,
                    get: () => true
                }
            );

        } catch(e) {}
    }

    // =========================
    // Neutralize blur
    // =========================

    window.onblur = null;
    document.onvisibilitychange = null;

    // =========================
    // Keep alive
    // =========================

    setInterval(() => {

        window.dispatchEvent(new Event('focus'));

        document.dispatchEvent(
            new Event('visibilitychange')
        );

    }, 15000);

})();