diff --git a/xExtension-YouTube/README.md b/xExtension-YouTube/README.md index 6da2f57a..dcb710ab 100644 --- a/xExtension-YouTube/README.md +++ b/xExtension-YouTube/README.md @@ -8,6 +8,7 @@ To use it, upload the ```xExtension-YouTube``` directory to the FreshRSS `./exte - Embeds Youtube videos directly in FreshRSS, instead of linking to the Youtube page - Simplifies the subscription to channel URLs by automatically detecting the channels feed URL +- Optionally shows only the video thumbnail, and loads the player once it is clicked, so that the pages load faster and the players do not contact YouTube until you decide to watch a video You can simply add Youtube video subscriptions by pasting URLs like: - `https://www.youtube.com/channel/UCwbjxO5qQTMkSZVueqKwxuw` @@ -23,6 +24,9 @@ With activated Youtube extension: ## Changelog +1.5.0: +- Added an option to only load the player once the video thumbnail is clicked + 1.4.1: - Fixed the original thumbnail, description and enclosures of the feed being appended below the video diff --git a/xExtension-YouTube/configure.phtml b/xExtension-YouTube/configure.phtml index 247800e2..153f1902 100644 --- a/xExtension-YouTube/configure.phtml +++ b/xExtension-YouTube/configure.phtml @@ -51,6 +51,13 @@ declare(strict_types=1); +
+ +
+
diff --git a/xExtension-YouTube/extension.php b/xExtension-YouTube/extension.php index 95b71a76..5c912187 100644 --- a/xExtension-YouTube/extension.php +++ b/xExtension-YouTube/extension.php @@ -37,6 +37,10 @@ final class YouTubeExtension extends Minz_Extension * Switch to enable the Youtube No-Cookie domain */ private bool $useNoCookie = false; + /** + * Whether a preview of the video is displayed instead of the player, which is only loaded once the preview is clicked + */ + private bool $clickToPlay = false; /** * Initialize this extension @@ -44,6 +48,7 @@ final class YouTubeExtension extends Minz_Extension #[\Override] public function init(): void { Minz_View::appendStyle($this->getFileUrl('style.css')); + Minz_View::appendScript($this->getFileUrl('clickToPlay.js')); $this->registerHook('entry_before_display', $this->embedYouTubeVideo(...)); $this->registerHook('check_url_before_add', self::convertYoutubeFeedUrl(...)); $this->registerHook('custom_favicon_hash', $this->iconHashParams(...)); @@ -355,6 +360,11 @@ public function loadConfigValues(): void { if ($noCookie !== null) { $this->useNoCookie = $noCookie; } + + $clickToPlay = FreshRSS_Context::userConf()->attributeBool('yt_click_to_play'); + if ($clickToPlay !== null) { + $this->clickToPlay = $clickToPlay; + } } /** @@ -413,6 +423,14 @@ public function isUseNoCookieDomain(): bool { return $this->useNoCookie; } + /** + * Returns whether a preview of the video is displayed instead of the player, until it is clicked. + * You have to call loadConfigValues() before this one, otherwise you get default values. + */ + public function isClickToPlay(): bool { + return $this->clickToPlay; + } + /** * Inserts the YouTube video iframe into the content of an entry, if the entries link points to a YouTube watch URL. * @throws FreshRSS_Context_Exception @@ -434,7 +452,8 @@ public function embedYouTubeVideo(FreshRSS_Entry $entry): FreshRSS_Entry { return $entry; } - if (stripos($entry->content(), ''; + if ($this->clickToPlay) { + $iframe = $this->getPreviewHtml($entry, $url) ?? $iframe; + } + if ($this->showContent) { $doc = new DOMDocument(); $doc->encoding = 'UTF-8'; @@ -641,6 +689,7 @@ public function handleConfigureAction(): void { FreshRSS_Context::userConf()->_attribute('yt_show_content', Minz_Request::paramBoolean('yt_show_content')); FreshRSS_Context::userConf()->_attribute('yt_download_channel_icons', Minz_Request::paramBoolean('yt_download_channel_icons')); FreshRSS_Context::userConf()->_attribute('yt_nocookie', Minz_Request::paramBoolean('yt_nocookie')); + FreshRSS_Context::userConf()->_attribute('yt_click_to_play', Minz_Request::paramBoolean('yt_click_to_play')); FreshRSS_Context::userConf()->save(); } diff --git a/xExtension-YouTube/i18n/de/ext.php b/xExtension-YouTube/i18n/de/ext.php index 05a76cd9..97d58f27 100644 --- a/xExtension-YouTube/i18n/de/ext.php +++ b/xExtension-YouTube/i18n/de/ext.php @@ -13,5 +13,6 @@ 'fetching_icons' => 'Fetching icons', 'finished_fetching_icons' => 'Finished fetching icons.', 'use_nocookie' => 'Verwende die Cookie-freie Domain www.youtube-nocookie.com', + 'click_to_play' => 'Den Player erst nach einem Klick auf das Vorschaubild laden', ), ); diff --git a/xExtension-YouTube/i18n/en/ext.php b/xExtension-YouTube/i18n/en/ext.php index 56a85ec1..445d3e13 100644 --- a/xExtension-YouTube/i18n/en/ext.php +++ b/xExtension-YouTube/i18n/en/ext.php @@ -13,5 +13,6 @@ 'fetching_icons' => 'Fetching icons', 'finished_fetching_icons' => 'Finished fetching icons.', 'use_nocookie' => 'Use the cookie-free domain www.youtube-nocookie.com', + 'click_to_play' => 'Only load the player once the video thumbnail is clicked', ), ); diff --git a/xExtension-YouTube/i18n/fr/ext.php b/xExtension-YouTube/i18n/fr/ext.php index 32f8937e..d63fde92 100644 --- a/xExtension-YouTube/i18n/fr/ext.php +++ b/xExtension-YouTube/i18n/fr/ext.php @@ -13,5 +13,6 @@ 'fetching_icons' => 'Récupération des icônes', 'finished_fetching_icons' => 'Récupération des icônes terminée.', 'use_nocookie' => 'Utiliser le domaine www.youtube-nocookie.com pour éviter les cookies', + 'click_to_play' => 'Ne charger le lecteur qu’après un clic sur la miniature de la vidéo', ), ); diff --git a/xExtension-YouTube/i18n/pl/ext.php b/xExtension-YouTube/i18n/pl/ext.php index 0f1f7768..fcf32d8b 100644 --- a/xExtension-YouTube/i18n/pl/ext.php +++ b/xExtension-YouTube/i18n/pl/ext.php @@ -13,5 +13,6 @@ 'fetching_icons' => 'Pobieranie ikon', 'finished_fetching_icons' => 'Zakończono pobieranie ikon.', 'use_nocookie' => 'Używaj domeny bez ciasteczek www.youtube-nocookie.com', + 'click_to_play' => 'Ładuj odtwarzacz dopiero po kliknięciu miniatury filmu', ), ); diff --git a/xExtension-YouTube/i18n/tr/ext.php b/xExtension-YouTube/i18n/tr/ext.php index 0d4e0434..70468359 100644 --- a/xExtension-YouTube/i18n/tr/ext.php +++ b/xExtension-YouTube/i18n/tr/ext.php @@ -13,5 +13,6 @@ 'fetching_icons' => 'Fetching icons', 'finished_fetching_icons' => 'Finished fetching icons.', 'use_nocookie' => 'Çerezsiz olan "www.youtube-nocookie.com" alan adını kullanın', + 'click_to_play' => 'Oynatıcıyı yalnızca video küçük resmine tıklandığında yükle', ), ); diff --git a/xExtension-YouTube/metadata.json b/xExtension-YouTube/metadata.json index 525d10b5..d47e9ec8 100644 --- a/xExtension-YouTube/metadata.json +++ b/xExtension-YouTube/metadata.json @@ -2,7 +2,7 @@ "name": "YouTube Video Feed", "author": "Kevin Papst, Inverle", "description": "Embed YouTube feeds inside article content.", - "version": "1.4.1", + "version": "1.5.0", "entrypoint": "YouTube", "type": "user" } diff --git a/xExtension-YouTube/static/clickToPlay.js b/xExtension-YouTube/static/clickToPlay.js new file mode 100644 index 00000000..8eb73483 --- /dev/null +++ b/xExtension-YouTube/static/clickToPlay.js @@ -0,0 +1,31 @@ +'use strict'; + +// Replace the preview of a video by its player once clicked, so that the player is only loaded when the user wants to watch the video +document.addEventListener('click', function (ev) { + if (ev.button !== 0 || ev.ctrlKey || ev.metaKey || ev.shiftKey || ev.altKey) { + return; // Keep the default behaviour of the link, e.g. to open the video in a new tab + } + const preview = ev.target.closest('a.yt-player-preview[data-yt-player-src]'); + if (!preview) { + return; + } + ev.preventDefault(); + + const src = new URL(preview.dataset.ytPlayerSrc, document.baseURI); + src.searchParams.set('autoplay', '1'); + + const iframe = document.createElement('iframe'); + iframe.className = 'youtube-plugin-video'; + if (preview.classList.contains('yt-player-autosize')) { + iframe.classList.add('yt-player-autosize'); + } else { + iframe.width = preview.dataset.ytPlayerWidth; + iframe.height = preview.dataset.ytPlayerHeight; + } + iframe.src = src.href; + iframe.setAttribute('frameborder', '0'); + iframe.referrerPolicy = 'strict-origin-when-cross-origin'; + iframe.allow = 'autoplay'; // Start the video, as it has just been clicked + iframe.allowFullscreen = true; + preview.replaceWith(iframe); +}); diff --git a/xExtension-YouTube/static/style.css b/xExtension-YouTube/static/style.css index e39adc12..f066c25c 100644 --- a/xExtension-YouTube/static/style.css +++ b/xExtension-YouTube/static/style.css @@ -2,3 +2,42 @@ width: 100%; aspect-ratio: 16 / 9; } + +.yt-player-preview { + display: block; + max-width: 100%; + width: fit-content; + position: relative; +} + +.yt-player-preview > img { + display: block; + height: auto; + aspect-ratio: 16 / 9; + object-fit: cover; +} + +.yt-player-preview.yt-player-autosize { + width: 100%; +} + +.yt-player-preview.yt-player-autosize > img { + width: 100%; + height: 100%; +} + +.yt-player-preview::after { + margin: auto; + background: url('data:image/svg+xml,') center / contain no-repeat; + width: 68px; + height: 48px; + position: absolute; + inset: 0; + content: ''; + opacity: 0.8; +} + +.yt-player-preview:hover::after, +.yt-player-preview:focus-visible::after { + opacity: 1; +}