Pause videos playing in background tabs when a video starts
A userscript that pauses background YouTube videos when a video starts playing in the active tab.
This prevents multiple videos from playing simultaneously across tabs.
anhkhoakz1.1.0anhkhoakz*://www.youtube.com/*document-startThe script leverages:
pause message is broadcast to other tabs.video.currentTime > 0!video.paused!video.endedvideo.readyState >= HAVE_ENOUGH_DATApopstate, pushstate, replacestate eventshistory.pushState / history.replaceStatesetInterval fallbackfindElement(selector: string): Promise<Element>Waits for a DOM element (YouTube video player) to appear before attaching listeners.
isVideoPlaying(video: HTMLVideoElement): booleanChecks if a video is actively playing.
isTabInFocus(): booleanReturns true if the current tab is active.
shouldPauseVideo(message: string, video: VideoWithIsPlaying): booleanDetermines if a background video should be paused when a pause message is received.
playing, loadeddata, canplay → Broadcast pause message when in focus.visibilitychange → Sends pause when tab regains focus.pushState, replaceState)setInterval check every secondpopstate, pushstate, replacestateBroadcastChannel("video-channel")pause messages and stop their video.1.1.0playing event.1.0.0QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址