UX: Fetch higher-resolution youtube thumbnails where available

We have been using YouTube's 'hqdefault.jpg' image which works consistently to provide a 480x360 thumbnail. YouTube does provide larger thumbnails, but not consistently for every video. By using og:image, we will fetch the best resolution YouTube can provide for each video.

This commit also refactors lazy-yt to re-use the thumbnail already existing in the cooked content. This means we get lazy-loading for free, and avoid hotlinking images to YouTube (when download remote images is enabled on the site).
This commit is contained in:
David Taylor
2020-05-15 17:49:27 +01:00
parent 7edd312f85
commit ac042c2bbe
3 changed files with 35 additions and 12 deletions

View File

@ -87,6 +87,8 @@
innerHtml.push("</div>"); // .html5-title-text-wrapper
innerHtml.push("</div>"); // end of Video title .html5-info-bar
const prefetchedThumbnail = $el[0].querySelector(".ytp-thumbnail-image");
$el
.css({
"padding-bottom": padding_bottom
@ -108,17 +110,27 @@
thumb_img = "default.jpg";
}
if (prefetchedThumbnail) {
$el.find(".ytp-thumbnail").append(prefetchedThumbnail);
} else {
// Fallback for old posts which were baked before the lazy-yt onebox prefetched a thumbnail
$el
.find(".ytp-thumbnail")
.append(
$(
[
'<img class="ytp-thumbnail-image" src="https://img.youtube.com/vi/',
id,
"/",
thumb_img,
'">'
].join("")
)
);
}
$thumb = $el
.find(".ytp-thumbnail")
.css({
"background-image": [
"url(https://img.youtube.com/vi/",
id,
"/",
thumb_img,
")"
].join("")
})
.addClass("lazyYT-image-loaded")
.on("click", function(e) {
e.preventDefault();