fix: prevent initial playback when autoplay is disabled (#1461)#3890
Open
jiangyj545 wants to merge 1 commit into
Open
fix: prevent initial playback when autoplay is disabled (#1461)#3890jiangyj545 wants to merge 1 commit into
jiangyj545 wants to merge 1 commit into
Conversation
…#1461) When opening a video in a new tab or via address bar, the video would play for ~1 second before being paused (adding it to watch history). This race condition occurred because YouTube's player started playback before our play() interception could fully take effect on fresh page loads. Fix: Add a 'playing' event listener as a safety net. When autoplay is disabled and no user interaction has occurred, immediately pause the video as soon as the 'playing' event fires. This catches any playback that slips through the HTMLMediaElement.prototype.play() interception gap. Changes: - New ImprovedTube.autoplayPlayingHandler() function that checks autoplay settings and pauses playback if disabled + no user interaction - Safety net 'playing' event listener registered in playerOnPlay() (self-removing after first fire to avoid overhead) - Covers all three autoplay-disable scenarios: player, playlist, channel trailer
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When Autoplay is disabled in ImprovedTube settings, opening a YouTube video in a new tab or via the address bar causes the video to play for ~1 second before being paused. This brief playback:
Root Cause
The existing fix hooks
HTMLMediaElement.prototype.play()and callsplayer.pauseVideo()immediately. However, on fresh page loads (new tab / address bar navigation), there is a race condition: YouTube's player begins rendering frames before our pause takes effect. The ~1 second gap is enough for YouTube to register the video as "played" and add it to watch history.Internal navigation works correctly because the player element (and our hook) is already initialized from the previous page.
Solution
Added a
playingevent safety net that catches any playback slipping through theplay()interception gap:New:
ImprovedTube.autoplayPlayingHandler()playingeventModified:
ImprovedTube.playerOnPlay()playingsafety-net listener alongside the existingplay()interceptionChanges
js&css/web-accessible/functions.jsTesting
npm test)Fixes #1461