Skip to content

Add YouTube Background Play userscript - #21

Open
VeigaPunk wants to merge 2 commits into
AdguardTeam:masterfrom
VeigaPunk:feat/youtube-background-play
Open

Add YouTube Background Play userscript#21
VeigaPunk wants to merge 2 commits into
AdguardTeam:masterfrom
VeigaPunk:feat/youtube-background-play

Conversation

@VeigaPunk

Copy link
Copy Markdown

What

Adds a new userscript, youtubeBackgroundPlay/youtube-background-play.user.js, that keeps YouTube and YouTube Music playing when the tab is hidden or the screen is locked.

It is a port of the well-known "YouTube Background Playback" Firefox fix, adapted for userscript managers (tested with AdGuard's userscript engine / Dguard).

How it works

  1. Spoofs the Page Visibility APIdocument.hidden, visibilityState (incl. vendor-prefixed variants) and hasFocus() always report visible/focused.
  2. Neutralizes visibility/focus eventsvisibilitychange, blur, focusout, pagehide, freeze are stopped at capture phase, and listener registration for them on window/document is swallowed so site pause logic never hooks them.
  3. Filters synthetic pause calls — wraps the player's pauseVideo() and HTMLMediaElement.pause() so programmatic pauses without a recent real user gesture (~1.5s window) are ignored, while genuine user pauses keep working.
  4. Keeps Media Session alive — lock-screen controls remain functional.
  5. Suppresses idle-watch heartbeat beacons that drive "Are you still watching?" style pauses.

Runs at document-start, @grant none, no external dependencies. Matches *.youtube.com, *.youtube-nocookie.com and music.youtube.com.

Metadata

@downloadURL/@updateURL follow the repo convention (raw GitHub URLs on master), so once merged, users get automatic updates from this repo.

Also published on Greasyfork for testing: https://greasyfork.org/en/scripts/589117-youtube-background-play-firefox-fix-port

License: MIT (compatible with the repo's GPLv3 as a contribution).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant