diff --git a/include/functions.php b/include/functions.php index cd9839c2..d43eec8e 100644 --- a/include/functions.php +++ b/include/functions.php @@ -238,7 +238,10 @@ function formatImg($src, $enableImageResizer, $image_max_width, $image_max_heigh if (empty($src)) { return ""; } - return addTempCode("\"image\""); + return addTempCode("\"image\""); } function formatFlash($src, $width, $height) { diff --git a/public/js/curtain_imageresizer.js b/public/js/curtain_imageresizer.js index bc8d6e31..3a7656d4 100644 --- a/public/js/curtain_imageresizer.js +++ b/public/js/curtain_imageresizer.js @@ -84,3 +84,22 @@ function Return() { $('curtain').style.display = "none"; $('lightbox').innerHTML = ""; } +// 处理图片加载失败的函数 +function handleImageError(img, currentSrc) { + if (!currentSrc.includes('doubanio.com')) { + return; + } + const domainList = ['img1.doubanio.com', 'img2.doubanio.com', 'img3.doubanio.com', 'img9.doubanio.com']; // 备用域名列表 + let index = 0; + function tryNextDomain() { + if (index >= domainList.length) { + return; + } + img.src = currentSrc.replace(/https:\/\/[a-zA-Z0-9.-]+\.doubanio\.com/, `https://${domainList[index]}`); + img.onload = () => { + img.onload = img.onerror = null; + }; + img.onerror = tryNextDomain; + } + tryNextDomain(); +} diff --git a/public/js/nexus.js b/public/js/nexus.js index c4ea6628..9a4435e7 100644 --- a/public/js/nexus.js +++ b/public/js/nexus.js @@ -46,6 +46,9 @@ jQuery(document).ready(function () { // preview function getPosition(e, position) { + if (!position) { + return {}; + } return { left: e.pageX + position.offsetX, top: e.pageY + position.offsetY, @@ -75,23 +78,44 @@ jQuery(document).ready(function () { // lazy load if ("IntersectionObserver" in window) { - const imgList = [...document.querySelectorAll('.nexus-lazy-load')] - var io = new IntersectionObserver((entries) =>{ - entries.forEach(entry => { - const el = entry.target - const intersectionRatio = entry.intersectionRatio - // console.log(`el, ${el.getAttribute('data-src')}, intersectionRatio: ${intersectionRatio}`) + const fallbackImage = 'pic/misc/spinner.svg'; + const domainList = ['img1.doubanio.com', 'img2.doubanio.com', 'img3.doubanio.com', 'img9.doubanio.com']; + const imgList = [...document.querySelectorAll('.nexus-lazy-load')]; + const loadedImages = {}; + const io = new IntersectionObserver((entries) => { + entries.forEach(entry => { + const el = entry.target; + const intersectionRatio = entry.intersectionRatio; if (intersectionRatio > 0 && intersectionRatio <= 1 && !el.classList.contains('preview')) { - // console.log(`el, ${el.getAttribute('data-src')}, loadImg`) - const source = el.dataset.src - el.src = source - el.classList.add('preview') + let src = el.dataset.src; + if (src && src.includes('doubanio.com') && src.includes('l_ratio_poster')) { + src = src.replace('l_ratio_poster', 's_ratio_poster'); + el.dataset.src = src; + } + el.src = src; + el.classList.add('preview'); + loadedImages[src] = true; + el.onload = el.onerror = () => io.unobserve(el); + el.onerror = () => handleImageError(el, src); } - el.onload = el.onerror = () => io.unobserve(el) - }) - }) - - imgList.forEach(img => io.observe(img)) + }); + }); + imgList.forEach(img => io.observe(img)); + function handleImageError(img, currentSrc) { + if (!currentSrc.includes('doubanio.com')) { + img.src = fallbackImage; + } else { + tryNextDomain(img, currentSrc, 0); + } + } + function tryNextDomain(img, currentSrc, index = 0) { + if (index >= domainList.length) { + img.src = fallbackImage; + return; + } + img.src = currentSrc.replace(/https:\/\/[a-zA-Z0-9.-]+\.doubanio\.com/, `https://${domainList[index]}`); + img.onerror = () => tryNextDomain(img, currentSrc, index + 1); + } } //claim