Google Video Playback Optimizer

Keep Google video playback active even when the tab is not in focus

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Google Video Playback Optimizer
// @namespace    http://tampermonkey.net/
// @version      2024-10-09
// @description  Keep Google video playback active even when the tab is not in focus
// @author       UniverseDev
// @license      GPL-3.0-or-later
// @match        https://www.google.com/search?*
// @icon         https://www.google.com/s2/favicons?domain=google.com&sz=64
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function keepPageVisible() {
        let hidden;
        if (typeof document.hidden !== "undefined") {
            hidden = "hidden";
        } else if (typeof document.msHidden !== "undefined") {
            hidden = "msHidden";
        } else if (typeof document.webkitHidden !== "undefined") {
            hidden = "webkitHidden";
        } else {
            console.warn('Google Video Playback Optimizer: Visibility API not supported');
            return;
        }

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

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

            console.log('Google Video Playback Optimizer: Page visibility override applied');
        } catch (error) {
            console.error('Google Video Playback Optimizer: Failed to override visibility properties', error);
        }
    }

    function checkAndActivateForVideo() {
        const videoElements = document.querySelectorAll('video');
        if (videoElements.length > 0) {
            keepPageVisible();
        }
    }

    function checkAndActivateVisibilityOverride() {
        const url = window.location.href;
        const fragment = window.location.hash;

        if (fragment.includes('vid:')) {
            keepPageVisible();
        } else {
            console.log('Google Video Playback Optimizer: Not a video page based on URL fragment.');
        }
    }

    setInterval(checkAndActivateForVideo, 2000);

    window.addEventListener('load', () => {
        checkAndActivateForVideo();
        checkAndActivateVisibilityOverride();
    });

    const originalPushState = history.pushState;
    const originalReplaceState = history.replaceState;

    history.pushState = function() {
        originalPushState.apply(this, arguments);
        checkAndActivateForVideo();
        checkAndActivateVisibilityOverride();
    };

    history.replaceState = function() {
        originalReplaceState.apply(this, arguments);
        checkAndActivateForVideo();
        checkAndActivateVisibilityOverride();
    };

    checkAndActivateForVideo();
    checkAndActivateVisibilityOverride();

})();