Auto Pause Background Videos

Automatically pause videos when tab is in background and resume when returning

当前为 2025-11-29 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto Pause Background Videos
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Automatically pause videos when tab is in background and resume when returning
// @author       You
// @match        *://*/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const videoStates = new Map(); // Track video playing states

    function getAllVideos() {
        return Array.from(document.querySelectorAll('video'));
    }

    function pauseAllVideos() {
        const videos = getAllVideos();
        console.log(`[Video Controller] Found ${videos.length} videos, pausing active ones...`);

        videos.forEach(video => {
            try {
                // Check if video is actually playing
                const isPlaying = !video.paused && !video.ended && video.readyState > 2;

                if (isPlaying) {
                    videoStates.set(video, true);
                    video.pause();
                    console.log('[Video Controller] Paused:', video.src || video.currentSrc || 'unknown source');
                } else if (!videoStates.has(video)) {
                    // Mark as not playing if we haven't tracked it yet
                    videoStates.set(video, false);
                }
            } catch (e) {
                console.error('[Video Controller] Error pausing video:', e);
            }
        });
    }

    function resumeVideos() {
        const videos = getAllVideos();
        console.log(`[Video Controller] Resuming previously playing videos...`);

        videos.forEach(video => {
            try {
                const wasPlaying = videoStates.get(video);

                if (wasPlaying === true) {
                    video.play().then(() => {
                        console.log('[Video Controller] Resumed:', video.src || video.currentSrc || 'unknown source');
                    }).catch(e => {
                        console.log('[Video Controller] Could not resume:', e.message);
                    });
                    videoStates.set(video, false); // Reset state
                }
            } catch (e) {
                console.error('[Video Controller] Error resuming video:', e);
            }
        });
    }

    // Listen for visibility changes
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            console.log('[Video Controller] Tab hidden - pausing videos');
            pauseAllVideos();
        } else {
            console.log('[Video Controller] Tab visible - resuming videos');
            // Small delay to ensure page is ready
            setTimeout(resumeVideos, 100);
        }
    });

    // Also listen for blur/focus events as backup
    window.addEventListener('blur', function() {
        if (!document.hidden) {
            console.log('[Video Controller] Window blur - pausing videos');
            pauseAllVideos();
        }
    });

    window.addEventListener('focus', function() {
        if (!document.hidden) {
            console.log('[Video Controller] Window focus - resuming videos');
            setTimeout(resumeVideos, 100);
        }
    });

    // Watch for new videos being added
    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            mutation.addedNodes.forEach(function(node) {
                if (node.tagName === 'VIDEO') {
                    console.log('[Video Controller] New video detected');
                } else if (node.querySelectorAll) {
                    const newVideos = node.querySelectorAll('video');
                    if (newVideos.length > 0) {
                        console.log(`[Video Controller] ${newVideos.length} new video(s) detected`);
                    }
                }
            });
        });
    });

    // Start observing
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    console.log('[Video Controller] Auto Pause Background Videos loaded and active');
    console.log('[Video Controller] Initial videos found:', getAllVideos().length);
})();