Last active
November 28, 2023 17:36
-
-
Save turnercore/721b3afc7dd09f380bb7b18a2d0b0091 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 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); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed it to be more reliable.