Skip to content

[xExtension-YouTube] Add an option to only load the player once clicked - #581

Open
NopeCharon wants to merge 2 commits into
FreshRSS:mainfrom
NopeCharon:youtube-click-to-play
Open

NopeCharon wants to merge 2 commits into
FreshRSS:mainfrom
NopeCharon:youtube-click-to-play

Conversation

@NopeCharon

@NopeCharon NopeCharon commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Every YouTube/PeerTube entry displayed embeds a player <iframe>, which loads the YouTube player, its scripts and tracking requests. With many video entries on a page (especially with the articles expanded), the page is slow to render and keeps sending requests, even for videos that are never watched (#236).

  • add an Only load the player once the video thumbnail is clicked checkbox to the configuration page (yt_click_to_play user attribute, disabled by default so existing installations keep their current behaviour)
  • when enabled, the extension inserts a link to the video showing its thumbnail and a play button instead of the player:
    • the thumbnail is the one of the entry (the media:thumbnail of YouTube feeds), or https://i.ytimg.com/vi/<id>/hqdefault.jpg for YouTube videos without one. If no thumbnail is available (e.g. a PeerTube entry without any), the player is embedded as before
    • the new static/clickToPlay.js replaces the preview by the usual player once clicked, with autoplay=1 (and allow="autoplay") so a single click starts the video. Control/command/shift/middle clicks keep the default link behaviour, e.g. to open the video in a new tab
    • the preview uses the configured width with a 16:9 ratio, or the responsive sizing when Automatically size the player is enabled. No inline styles are used, because of the CSP of FreshRSS
    • the thumbnail goes through the lazy loading of FreshRSS and through ImageProxy, like any other image of the content
    • without JavaScript, e.g. in API clients, it degrades to a thumbnail linking to the video
  • the check preventing the player from being inserted twice now matches class="youtube-plugin-video instead of <iframe class="youtube-plugin-video", so it also matches the preview (and the responsive player, which it did not match before)
  • translations for all supported languages, README feature list and changelog, version 1.6.0

Fixes #236

Validation

End-to-end with FreshRSS edge (PHP 8.4, SQLite), the extension enabled, and a local feed containing a YouTube video with a media:group thumbnail, a Short without a thumbnail, and a PeerTube video without a thumbnail:

  • option disabled: same output as on main (iframes)
  • option enabled, fixed size 640×480, articles expanded: the two YouTube entries show the preview (thumbnail of the feed, i.ytimg.com fallback for the Short) and the PeerTube entry the iframe. In headless Chromium, a click replaces the preview by <iframe class="youtube-plugin-video" width="640" height="480" src="https://www.youtube.com/embed/aqz-KE-bpKQ?autoplay=1" …> without leaving the page, and a Control+click opens the video in a new tab and keeps the preview
  • option enabled with Automatically size the player and Display the feeds content, articles collapsed (lazy loading): after opening the article, the preview is full width at 16:9 with the description below it, and the click gives the responsive player at the same size
  • titles with quotes, & or < are encoded once in the alt attribute of the thumbnail
  • the configuration form shows the checkbox and saves it
  • php-lint, phtml-lint, phpcs, phpstan (with ../FreshRSS), eslint, stylelint and markdownlint

Every YouTube (or PeerTube) entry displayed embeds a player <iframe>,
which loads the YouTube player, its scripts and its tracking requests.
With many video entries on a page, this makes the page slow to render
and floods the browser with requests, even for videos that the user
never watches.

Add an "Only load the player once the video thumbnail is clicked"
checkbox to the configuration page, stored in the `yt_click_to_play`
user attribute. When it is enabled, the extension inserts a link to the
video showing its thumbnail instead of the player:

- the thumbnail is the one of the entry (e.g. the `media:thumbnail` of
  YouTube feeds), or `i.ytimg.com/vi/<id>/hqdefault.jpg` for YouTube
  videos without one. When no thumbnail is available (e.g. a PeerTube
  entry without any), the player is embedded as before
- the new `static/clickToPlay.js` replaces the preview by the usual
  player once clicked, with `autoplay=1` so that a single click starts
  the video. Control/command/shift/middle clicks keep the default link
  behaviour, e.g. to open the video in a new tab
- the preview uses the configured width with a 16:9 ratio, or the
  responsive sizing when enabled, and shows a play button
- without JavaScript, e.g. in API clients, it degrades to a thumbnail
  linking to the video

The check preventing the player from being inserted twice now also
matches the preview (and the responsive player, whose class attribute
did not match before).

The option is disabled by default, so existing installations keep their
current behaviour. Add translations for all supported languages, and
bump the extension version to 1.6.0.

Fixes FreshRSS#236
@NopeCharon
NopeCharon force-pushed the youtube-click-to-play branch from 7a65e3c to bb24876 Compare December 5, 2026 19:12
Comment thread xExtension-YouTube/static/style.css Outdated

.yt-player-preview::after {
margin: auto;
background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 68 48"><rect width="68" height="48" rx="12" fill="%23000" fill-opacity=".8"/><path d="M27 14v20l18-10z" fill="%23fff"/></svg>') center / contain no-repeat;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would prefer if the SVG was put into its own file, instead of embedding it in the CSS directly with data:.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved it to static/play.svg, and the stylesheet now loads it through ext.php in d368789.

}
iframe.src = src.href;
iframe.setAttribute('frameborder', '0');
iframe.referrerPolicy = 'strict-origin-when-cross-origin';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Make sure to do this only when a YouTube domain is in the referrer allowlist (there's a configuration setting for that in FreshRSS). Optionally, you could also suggest to add the domain for the user to the allowlist automatically.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The referrer policy is now only set when the iframe host is in send_referrer_allowlist, same check as enforce_referrer_allowlist() in FreshRSS (d368789).

@Inverle

Inverle commented Dec 5, 2026

Copy link
Copy Markdown
Member

@NopeCharon I left the review comments a while ago, but forgot to submit them or something (they didn't display apparently). Sorry for the delay.

@Inverle Inverle added the xExtension-YouTube xExtension-YouTube label Dec 5, 2026
… the referrer allowlist

Move the play button of the video preview from a data: URI in the
stylesheet to static/play.svg, referenced relatively to ext.php which
serves the stylesheet.

When the preview is clicked, only set the referrer policy of the player
<iframe> if its host is in the "Sites allowed to see your server
address" privacy setting of FreshRSS, like FreshRSS does for the
<iframe> of the content. The setting does not exist before FreshRSS
1.29, in which case the referrer policy of the page applies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

xExtension-YouTube xExtension-YouTube

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[xExtension-YouTube] Loop loading entries

2 participants