close
Skip to content

Fix: Add smart hardware check and network probe for HD thumbnails#3770

Open
AryaXDG wants to merge 1 commit intocode-charity:masterfrom
AryaXDG:fix/thumbnail-grid-layout
Open

Fix: Add smart hardware check and network probe for HD thumbnails#3770
AryaXDG wants to merge 1 commit intocode-charity:masterfrom
AryaXDG:fix/thumbnail-grid-layout

Conversation

@AryaXDG
Copy link
Copy Markdown
Member

@AryaXDG AryaXDG commented Apr 10, 2026

The PR addresses network rejection errors (404s) and significant data waste associated with the "HD Thumbnail" and "Thumbnails Quality" features.

Currently, the extension uses a "blind swap" approach to upgrade thumbnails, forcefully replacing hqdefault.jpg strings with maxresdefault.jpg (1280px). If YouTube has not generated a high-res version for a specific video (very common for older uploads), the server rejects the request (404 Not Found), resulting in broken images, black boxes, and layout breaking. Furthermore, applying massive 1280px images to physically small screens (like smartphones) wastes significant bandwidth and battery life, as the required pixel density far exceeds what the human eye can perceive at normal viewing distances.

This patch refactors the thumbnail upgrade logic to use a "Smart Hardware & Network Probe" architecture. It dynamically evaluates the user's physical screen resolution before offering the features in the UI, and silently pre-loads images to ensure they exist on YouTube's servers before altering the DOM.

Changes Included

  • Network Pre-loader Probe: Replaced the legacy blind string replacement in appearance.js with a background Image() object probe. The engine now silently tests if maxresdefault.jpg exists. If YouTube returns a 404, it safely aborts and leaves hqdefault.jpg intact, completely eliminating broken thumbnail layouts.
  • Hardware Context Check: Implemented a mathematical check (window.screen.width * window.devicePixelRatio) to determine the absolute physical pixel count of the user's display.
  • Dynamic UI Optimization: Added on: { render: } logic to the Satus menu components in appearance.js and general.js. If the user's physical screen width is below 1000px (e.g., mobile phones, older tablets), the "HD Thumbnail" switch and "Thumbnails Quality" dropdown are completely hidden. This prevents users from activating data-heavy features their hardware cannot physically render.
  • Targeted DOM Scaling: Adjusted the core function (ImprovedTube.playerHdThumbnail) to accept specific element parameters (thumbnailElement || ImprovedTube.elements.player_thumbnail), ensuring the new smart logic scales safely across both the main video player and modern grid layouts.

var rect = thumbnail.getBoundingClientRect();
var physicalWidth = (rect.width || thumbnail.clientWidth || 0) * (window.devicePixelRatio || 1);

if (physicalWidth > 400 || physicalWidth === 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When physicalWidth === 0 the thumbnail has no layout (hidden, not in DOM, or mid-navigation). Rather than skipping safely, the probe fires anyway. This is the condition most likely to occur during SPA navigation when the player is being rebuilt. The probe result arrives after the fact and may apply to a stale DOM node.

Fix: Treat zero as "unknown/unsafe" and skip

@ImprovedTube ImprovedTube force-pushed the master branch 4 times, most recently from d4783a9 to 0403173 Compare April 20, 2026 07:42
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.

2 participants