Skip to content

Instantly share code, notes, and snippets.

@turnercore
Created November 28, 2023 17:35
Show Gist options
  • Save turnercore/ae86f70357c78a500071d855c07a4792 to your computer and use it in GitHub Desktop.
Save turnercore/ae86f70357c78a500071d855c07a4792 to your computer and use it in GitHub Desktop.
Stop YouTube Play on Load
(() => {
let isInitialized = false; // Flag to indicate if the initial pause has been done
const pauseVideo = () => {
if (isInitialized) return; // If the initial pause has been done, do nothing further
const video = document.querySelector('#movie_player > div.html5-video-container > video');
// Pause the video if it's found and playing, and then perform cleanup
if (video && !video.paused) {
video.pause();
isInitialized = true;
cleanup(); // Clean up immediately after pausing
}
};
const videoCheckInterval = setInterval(pauseVideo, 1000);
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.matches('#movie_player > div.html5-video-container > video')) {
pauseVideo();
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
const cleanup = () => {
clearInterval(videoCheckInterval);
observer.disconnect();
window.removeEventListener('beforeunload', cleanup);
};
window.addEventListener('beforeunload', cleanup);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment