Stop Video Light

Stops all videos on the page

目前为 2024-02-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         Stop Video Light
// @namespace    Stop Video Light
// @version      1.0
// @description  Stops all videos on the page
// @author       Nameniok
// @match        *://*/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const config = {
        blockVideoPreload: true,      // Stop video loading
        blockAutoplay: true,          // Stop automatic playback
    };

    function stopAndDisablePreload(video) {
        video.pause();
        video.removeAttribute('preload');
    }
    function stopAndDisablePreloadForAllVideos() {
    Array.from(document.querySelectorAll('video')).forEach(video => {
        stopAndDisablePreload(video);
        addCanPlayListener(video);
        applyAdditionalSettings(video);

        video.addEventListener('loadedmetadata', () => {
            stopAndDisablePreload(video);
            addCanPlayListener(video);
            applyAdditionalSettings(video);
        });
    });
}
function addCanPlayListener(video) {
    video.addEventListener('canplay', () => {
        stopAndDisablePreload(video);
    }, { once: true });

    video.addEventListener('loadedmetadata', () => {
        stopAndDisablePreload(video);
    }, { once: true });
}
    function observeVideos(mutationsList) {
        mutationsList.forEach(mutation => {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                mutation.addedNodes.forEach(node => {
                    if (node.tagName && node.tagName.toLowerCase() === 'video') {
                        if (config.blockVideoPreload) {
                            stopAndDisablePreload(node);
                        }
                        if (config.blockAutoplay) {
                            addCanPlayListener(node);
                        }
                        applyAdditionalSettings(node);
                    } else if (node.querySelectorAll) {
                        Array.from(node.querySelectorAll('video')).forEach(video => {
                            if (config.blockVideoPreload) {
                                stopAndDisablePreload(video);
                            }
                            if (config.blockAutoplay) {
                                addCanPlayListener(video);
                            }
                            applyAdditionalSettings(video);
                        });
                    }
                });
            }
        });
    }
    function initObserver() {
        const observer = new MutationObserver(observeVideos);
        const targetNode = document.body;

        const observerConfig = {
            childList: true,
            subtree: true
        };
        observer.observe(targetNode, observerConfig);
    }
    window.addEventListener('load', () => {
        stopAndDisablePreloadForAllVideos();
        initObserver();
    });
})();