Skip to content

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

Draft
jiangyj545 wants to merge 1 commit into
code-charity:masterfrom
jiangyj545:fix/autoplay-block-1461
Draft

fix: block autoplay before any frame renders (#1461)#3911
jiangyj545 wants to merge 1 commit into
code-charity:masterfrom
jiangyj545:fix/autoplay-block-1461

Conversation

@jiangyj545

Copy link
Copy Markdown

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.

- Extract blockAutoplay() helper: pauseVideo() + pause() fallback + currentTime reset
- Add 'playing' event safety net with once:true to catch playback that slips through
- Reduce setTimeout delay from 100ms to 0 to minimize rendered frames
- Reset currentTime=0 so no progress is recorded in watch history

Fixes code-charity#1461: Video was playing ~1 second before pause; now blocked before first frame.
@ImprovedTube2 ImprovedTube2 marked this pull request as draft June 3, 2026 04:22
@ImprovedTube2

Copy link
Copy Markdown
Collaborator

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants