Stops HTML5 video autoplay on most websites, even after dynamic page changes
// ==UserScript==
// @name Disable HTML5 Video Autoplay
// @namespace RGlzYWJsZSBIVE1MNSBWaWRlbyBBdXRvcGxheQ
// @version 1.2
// @description Stops HTML5 video autoplay on most websites, even after dynamic page changes
// @author smed79
// @license GPLv3
// @icon https://i25.servimg.com/u/f25/11/94/21/24/noplay10.png
// @homepage https://greasyfork.org/en/scripts/543737-disable-html5-video-autoplay
// @include http://*
// @include https://*
// @grant none
// ==/UserScript==
(function () {
'use strict';
let observer;
const pauseVideoOnce = () => {
const video = document.querySelector('video');
if (video && !video.paused && video.currentTime < 2) {
video.pause();
if (observer) observer.disconnect();
}
};
const waitForVideo = () => {
if (observer) observer.disconnect();
observer = new MutationObserver(() => {
pauseVideoOnce();
});
observer.observe(document.body, { childList: true, subtree: true });
};
const hookNavigation = () => {
let lastUrl = location.href;
const navObserver = new MutationObserver(() => {
const currentUrl = location.href;
if (currentUrl !== lastUrl) {
lastUrl = currentUrl;
setTimeout(waitForVideo, 500);
}
});
navObserver.observe(document.body, { childList: true, subtree: true });
};
waitForVideo();
hookNavigation();
window.addEventListener('yt-navigate-finish', () => {
setTimeout(waitForVideo, 500);
});
})();