Skip to content

fix: prevent initial playback when autoplay is disabled (#1461)#3890

Open
jiangyj545 wants to merge 1 commit into
code-charity:masterfrom
jiangyj545:fix/autoplay-prevent-initial-playback-1461
Open

fix: prevent initial playback when autoplay is disabled (#1461)#3890
jiangyj545 wants to merge 1 commit into
code-charity:masterfrom
jiangyj545:fix/autoplay-prevent-initial-playback-1461

Conversation

@jiangyj545
Copy link
Copy Markdown

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:

  • Adds the video to watch history (the core user complaint)
  • Defeats the purpose of the autoplay-disable setting
  • Does not occur on internal YouTube navigation (clicking video links within YouTube)

Root Cause

The existing fix hooks HTMLMediaElement.prototype.play() and calls player.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 playing event safety net that catches any playback slipping through the play() interception gap:

New: ImprovedTube.autoplayPlayingHandler()

  • Fires on the native HTMLMediaElement playing event
  • Checks all three autoplay-disable scenarios: player, playlist, channel trailer
  • Pauses immediately if autoplay is disabled AND user has not interacted
  • Self-removing listener (fires once, then detaches — zero ongoing overhead)

Modified: ImprovedTube.playerOnPlay()

  • Registers the playing safety-net listener alongside the existing play() interception
  • Only attached when autoplay conditions are met (no impact when autoplay is enabled)

Changes

File Change
js&css/web-accessible/functions.js +58 lines (new handler), -30 lines (reformatted)

Testing

  • ✅ All 61 existing unit tests pass (npm test)
  • Manual verification steps:
    1. Enable "Autoplay" -> OFF in ImprovedTube settings
    2. Open any YouTube video in a new tab -> video should not play at all
    3. Navigate via address bar -> same result
    4. Internal navigation (clicking video links) -> still works correctly
    5. Normal playback (autoplay ON) -> no regression

Fixes #1461

…#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
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.

Disabled autoplay pauses a second into video instead of immediately preventing playback

1 participant