Created
November 28, 2023 17:35
-
-
Save turnercore/ae86f70357c78a500071d855c07a4792 to your computer and use it in GitHub Desktop.
Stop YouTube Play on Load
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
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