Auto Pause/Resume Background Videos

Automatically pauses videos in background tabs and resumes when tab is active

目前為 2025-11-04 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto Pause/Resume Background Videos
// @namespace    kio
// @version      1.2
// @description  Automatically pauses videos in background tabs and resumes when tab is active
// @author       Kio
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const videos = new Set();

    function handleVideo(video) {
        if (videos.has(video)) return;
        videos.add(video);

        Object.defineProperty(video, 'isplaying', {
            get: function(){
                return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
            }
        });

        // Pause if tab not visible
        if (document.hidden && video.isplaying) video.pause();

        // Listen to tab visibility changes
        document.addEventListener('visibilitychange', () => {
            if (document.hidden) {
                if (video.isplaying) video.pause();
            } else {
                // Resume when tab active
                if (!video.isplaying && video.readyState > 2) {
                    video.play().catch(()=>{}); // catch error if autoplay blocked
                }
            }
        });
    }

    // Observe new video elements dynamically
    const observer = new MutationObserver((mutations) => {
        mutations.forEach(m => {
            m.addedNodes.forEach(node => {
                if (node.tagName === 'VIDEO') handleVideo(node);
                else if (node.querySelectorAll) {
                    node.querySelectorAll('video').forEach(v => handleVideo(v));
                }
            });
        });
    });

    observer.observe(document, { childList: true, subtree: true });

    // Handle existing videos
    document.querySelectorAll('video').forEach(v => handleVideo(v));
})();