Skip to content

Instantly share code, notes, and snippets.

@turnercore
Last active November 28, 2023 17:36
Show Gist options
  • Save turnercore/721b3afc7dd09f380bb7b18a2d0b0091 to your computer and use it in GitHub Desktop.
Save turnercore/721b3afc7dd09f380bb7b18a2d0b0091 to your computer and use it in GitHub Desktop.
Stop YouTube Play on Load
(() => {
let isVideoHandled = false; // Flag to indicate if the video has been handled
const checkVideoStart = () => {
const video = document.querySelector('#movie_player > div.html5-video-container > video');
if (!video) return;
// When the video starts playing (currentTime changes from 0), pause it
if (video.currentTime > 0 && !video.paused) {
video.pause();
isVideoHandled = true;
clearInterval(checkInterval); // Clear the interval after handling the video
}
};
// Interval to periodically check if the video has started
const checkInterval = setInterval(checkVideoStart, 100);
// Cleanup function to clear the interval when the window is closed or navigated away
const cleanup = () => {
clearInterval(checkInterval);
window.removeEventListener('beforeunload', cleanup);
};
window.addEventListener('beforeunload', cleanup);
})();
@turnercore
Copy link
Author

Fixed it to be more reliable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment