Skip to content

Instantly share code, notes, and snippets.

@turnercore
Last active December 21, 2024 09:30
Show Gist options
  • Select an option

  • Save turnercore/721b3afc7dd09f380bb7b18a2d0b0091 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
Author

Fixed it to be more reliable.

@mufidu
Copy link
Copy Markdown

mufidu commented Dec 10, 2024

Works so well! Thanks.

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