youtube icon indicating copy to clipboard operation
youtube copied to clipboard

fix: block autoplay before any frame renders (#1461)

Open jiangyj545 opened this issue 6 months ago • 1 comments

Summary

Fixes #1461 — Disabled autoplay was pausing ~1 second into playback instead of preventing it entirely.

Root Cause

The original autoplay-disable code:

  1. Intercepted HTMLMediaElement.prototype.play() (worked)
  2. Called player.pauseVideo() then returned Promise.resolve() (worked)
  3. BUT: YouTube's player found other paths to start playback (internal API, retry logic, etc.)
  4. The setTimeout(100ms) fallback allowed ~6 frames to render before pausing

Changes

File: js&css/web-accessible/functions.js (1 file, +22/-7)

Change Before After
Pause logic Inline try/catch Extracted blockAutoplay() helper
Safety net None playing event listener (once:true) catches escaped playback
setTimeout delay 100ms 0ms (next tick)
currentTime reset None Resets to 0 so watch history gets no progress

How the safety net works

play() called → blockAutoplay() pauses + resets time → return Promise.resolve()
                                                    ↓ (if YouTube retries)
                              'playing' fires → blockAutoplay() again → listener removes itself

The once:true ensures normal user-initiated playback is never affected.

Test Results

Test Suites: 16 passed, 1 failed (pre-existing)
Tests:       66 passed, 3 failed (pre-existing, from sidebar-transcript PR)

No new test failures introduced.


Bounty claim: This PR addresses issue #1461 (Bounty label). Requesting review.

jiangyj545 avatar May 31 '26 22:05 jiangyj545

hi! @jiangyj545 did you consider to make your (automated) process look at the timeline / edit history? thanks

ImprovedTube2 avatar Jun 03 '26 04:06 ImprovedTube2