Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions xExtension-YouTube/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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

Expand Down
7 changes: 7 additions & 0 deletions xExtension-YouTube/configure.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ declare(strict_types=1);
</label>
</div>

<div class="group-controls">
<label class="checkbox" for="yt_click_to_play">
<input type="checkbox" id="yt_click_to_play" name="yt_click_to_play" <?= $this->isClickToPlay() ? 'checked="checked"' : '' ?> />
<?= _t('ext.yt_videos.click_to_play') ?>
</label>
</div>

<div class="group-controls">
<button id="yt_action_btn" name="yt_action_btn" value="iconFetchFinish" type="submit" class="btn" disabled="disabled" title="<?= _t('gen.js.should_be_activated') ?>"><?= _t('ext.yt_videos.fetch_channel_icons') ?></button>
<button id="yt_action_btn" name="yt_action_btn" value="resetIcons" type="submit" class="btn" disabled="disabled"><?= _t('ext.yt_videos.reset_channel_icons') ?></button>
Expand Down
53 changes: 51 additions & 2 deletions xExtension-YouTube/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ 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
*/
#[\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(...));
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -434,7 +452,8 @@ public function embedYouTubeVideo(FreshRSS_Entry $entry): FreshRSS_Entry {
return $entry;
}

if (stripos($entry->content(), '<iframe class="youtube-plugin-video"') !== false) {
// The player (or its preview) has already been inserted
if (stripos($entry->content(), 'class="youtube-plugin-video') !== false) {
return $entry;
}

Expand Down Expand Up @@ -506,7 +525,32 @@ private function getEnclosureReferences(FreshRSS_Entry $entry): string {
}

/**
* Returns an HTML <iframe> for a given URL for the configured width and height, with content ignored, appended or formatted.
* Returns a link to the video showing its thumbnail, for the configured width (and a 16:9 ratio) or with responsive sizing,
* which `static/clickToPlay.js` replaces by the player <iframe> for the given URL once clicked.
* Returns null if there is no thumbnail to display.
*/
private function getPreviewHtml(FreshRSS_Entry $entry, string $url): ?string {
$thumbnail = $entry->thumbnail()['url'] ?? '';
if ($thumbnail === '' && preg_match('#^https://www\.youtube(?:-nocookie)?\.com/embed/([0-9A-Za-z_-]+)#', $url, $matches) === 1) {
$thumbnail = 'https://i.ytimg.com/vi/' . $matches[1] . '/hqdefault.jpg';
}
if ($thumbnail === '') {
return null;
}

$previewAttributes = $this->autoSize
? 'class="youtube-plugin-video yt-player-preview yt-player-autosize"'
: 'class="youtube-plugin-video yt-player-preview" data-yt-player-width="' . $this->width . '" data-yt-player-height="' . $this->height . '"';
$imageAttributes = 'alt="' . htmlspecialchars($entry->title(), ENT_COMPAT, 'UTF-8', double_encode: false) . '"' .
($this->autoSize ? '' : ' width="' . $this->width . '"');

return '<a ' . $previewAttributes . ' href="' . $entry->link() . '" data-yt-player-src="' . $url . '">' .
'<img src="' . $thumbnail . '" ' . $imageAttributes . ' /></a>';
}

/**
* Returns an HTML <iframe> (or a preview of the video replaced by the <iframe> once clicked) for a given URL
* for the configured width and height, with content ignored, appended or formatted.
*/
public function getHtml(FreshRSS_Entry $entry, string $url): string {
$content = '';
Expand All @@ -520,6 +564,10 @@ public function getHtml(FreshRSS_Entry $entry, string $url): string {
referrerpolicy="strict-origin-when-cross-origin"
allowFullScreen></iframe>';

if ($this->clickToPlay) {
$iframe = $this->getPreviewHtml($entry, $url) ?? $iframe;
}

if ($this->showContent) {
$doc = new DOMDocument();
$doc->encoding = 'UTF-8';
Expand Down Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions xExtension-YouTube/i18n/de/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);
1 change: 1 addition & 0 deletions xExtension-YouTube/i18n/en/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);
1 change: 1 addition & 0 deletions xExtension-YouTube/i18n/fr/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);
1 change: 1 addition & 0 deletions xExtension-YouTube/i18n/pl/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);
1 change: 1 addition & 0 deletions xExtension-YouTube/i18n/tr/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);
2 changes: 1 addition & 1 deletion xExtension-YouTube/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
31 changes: 31 additions & 0 deletions xExtension-YouTube/static/clickToPlay.js
Original file line number Diff line number Diff line change
@@ -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);
});
39 changes: 39 additions & 0 deletions xExtension-YouTube/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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,<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;
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;
}