// {
// Skip if we've already fixed it or are currently processing it
if (thumb.dataset.status) return;
let parentLink = thumb.closest('a');
if (!parentLink || !parentLink.href) return;
// Ensure it's a standard post URL
if (!parentLink.href.includes('.html')) return;
// Mark as processing so we don't fetch the same page twice
thumb.dataset.status = "processing";
// Fetch the literal HTML of the post page in the background
fetch(parentLink.href)
.then(response => response.text())
.then(html => {
// Regex catches youtube.com, youtu.be, nocookie, and shorts
let ytMatch = html.match(/(?:youtube(?:-nocookie)?\.com\/(?:embed\/|v\/|watch\?v=|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/);
if (ytMatch && ytMatch[1]) {
let videoId = ytMatch[1];
// Switch to i.ytimg.com (YouTube's primary CDN, often more reliable in UK/EU)
let hqThumb = "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg";
let mqThumb = "https://i.ytimg.com/vi/" + videoId + "/mqdefault.jpg";
// The Preloader & Fallback Tester
let imgChecker = new Image();
imgChecker.onload = function() {
// HQ exists and isn't blocked, apply it!
thumb.style.backgroundImage = "url('" + hqThumb + "')";
thumb.dataset.status = "done";
};
imgChecker.onerror = function() {
// HQ failed (404 or CDN block), instantly fall back to MQ
console.log("HQ thumbnail missing/blocked for " + videoId + ", using mqdefault");
thumb.style.backgroundImage = "url('" + mqThumb + "')";
thumb.dataset.status = "done-fallback";
};
// Trigger the check
imgChecker.src = hqThumb;
} else {
// No video found in this post
thumb.dataset.status = "no-video";
}
})
.catch(err => {
console.log("Failed to fetch post HTML for " + parentLink.href, err);
thumb.dataset.status = "error";
});
});
}
// Run once on load
fixThumbnails();
// Check every 2 seconds for dynamically loaded UI elements
setInterval(fixThumbnails, 2000);
});
//]]>