Repository navigation
[xExtension-YouTube] Add an option to only load the player once clicked - #581
NopeCharon wants to merge 2 commits into
Conversation
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
7a65e3c to
bb24876
Compare
|
|
||
| .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; |
There was a problem hiding this comment.
I would prefer if the SVG was put into its own file, instead of embedding it in the CSS directly with data:.
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
|
@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. |
… 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.
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).yt_click_to_playuser attribute, disabled by default so existing installations keep their current behaviour)media:thumbnailof YouTube feeds), orhttps://i.ytimg.com/vi/<id>/hqdefault.jpgfor YouTube videos without one. If no thumbnail is available (e.g. a PeerTube entry without any), the player is embedded as beforestatic/clickToPlay.jsreplaces the preview by the usual player once clicked, withautoplay=1(andallow="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 tabclass="youtube-plugin-videoinstead of<iframe class="youtube-plugin-video", so it also matches the preview (and the responsive player, which it did not match before)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 amedia:groupthumbnail, a Short without a thumbnail, and a PeerTube video without a thumbnail:main(iframes)i.ytimg.comfallback 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&or<are encoded once in thealtattribute of the thumbnailphp-lint,phtml-lint,phpcs,phpstan(with../FreshRSS),eslint,stylelintandmarkdownlint