Stops all videos on the page
当前为
// ==UserScript==
// @name Stop all videos
// @namespace Stop all videos
// @version 1.1
// @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();
});
})();