Skip to content

Fix: Resolves Firefox thumbnail not loading issue - #3691

Merged
ImprovedTube merged 1 commit into
code-charity:masterfrom
AryaXDG:fix/firefox-thumbnail
Mar 9, 2026
Merged

ImprovedTube merged 1 commit into
code-charity:masterfrom
AryaXDG:fix/firefox-thumbnail

Conversation

@AryaXDG

@AryaXDG AryaXDG commented Mar 9, 2026

Copy link
Copy Markdown
Member

Fixes: #3235, #2310

Root Cause of the Bug
The issue where thumbnails fail to load indefinitely in Firefox-based browsers is caused by direct DOM mutation interacting with Firefox's Enhanced Tracking Protection (ETP) and strict CORS policies. The previous logic directly replaced the src attribute of the live <img> element. When Firefox blocked the modified high-resolution image request, the failure occurred silently at the network level without triggering the standard onerror DOM event. Consequently, the fallback mechanism failed to execute, leaving the user with a permanently broken thumbnail.

Fixes and Architectural Improvements

  • Implemented Off-Screen Preloading: The logic now uses an off-screen Image() object to fetch and test the high-resolution URL asynchronously.
    This ensures the live page is never put at risk. The visible thumbnail's src is only swapped if the background download is strictly successful. If the browser blocks the request or the image is missing, the original thumbnail remains untouched.

  • Video ID Identity Verification: Replaced the fragile string-matching logic (which checked URLs for quality tokens like hqdefault) with a robust regex that extracts the unique 11-character YouTube Video ID.
    YouTube's Single Page Application (SPA) natively recycles <img> tags for new videos. Comparing exact Video IDs prevents "poisoned state" glitches where recycled DOM nodes would refuse to update.

  • Race Condition Prevention: Added a strict state check (thumbnail.dataset.defaultSrc === originalSrc) inside the asynchronous image load callback.
    This guarantees that rapid user scrolling does not cause a delayed high-resolution image from a previous video to overwrite the thumbnail of a newly recycled DOM node.

  • Infinite Scroll Support: Added explicit handling for childList mutations within the MutationObserver.
    The previous observer ignored newly injected DOM nodes. This addition ensures thumbnails are upgraded instantly as new videos are loaded into the page during downward scrolling.

  • Stale Closure Resolution: The observer is now actively disconnected and re-instantiated during execution.
    This resolves a bug where the observer was permanently trapped in a stale closure, causing it to ignore user configuration changes until a full page reload.

  • Query Parameter Stripping: The logic now strips trailing query parameters (e.g., ?sqp=...) from the base URL prior to string replacement.
    YouTube servers frequently reject maxresdefault upgrades if the request carries cryptographic signatures meant for lower-resolution images. Stripping these significantly increases the upgrade success rate.

@AryaXDG

AryaXDG commented Mar 9, 2026

Copy link
Copy Markdown
Member Author

@ImprovedTube
The Jest and ESLint CI checks are failing, but these appear to be pre-existing configuration issues on the main branch (a Node environment window error in the playlist test, and a YAML parsing error on the .mjs eslint config). They are not related to the changes in this PR.

@ImprovedTube
ImprovedTube merged commit c22e614 into code-charity:master Mar 9, 2026
1 check failed
@AryaXDG
AryaXDG deleted the fix/firefox-thumbnail branch April 10, 2026 18:01
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