Skip to content

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

Open
NopeCharon wants to merge 1 commit into
FreshRSS:mainfrom
NopeCharon:youtube-click-to-play
Open

NopeCharon wants to merge 1 commit into
FreshRSS:mainfrom
NopeCharon:youtube-click-to-play

Conversation

@NopeCharon

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.5.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.5.0.

Fixes FreshRSS#236
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.

[xExtension-YouTube] Loop loading entries

1 participant