修复:豆瓣图裂问题

This commit is contained in:
xqw8889
2025-11-11 22:20:54 +08:00
parent 6fb7be9036
commit 7d02a08c2d
3 changed files with 62 additions and 16 deletions

View File

@@ -238,7 +238,10 @@ function formatImg($src, $enableImageResizer, $image_max_width, $image_max_heigh
if (empty($src)) {
return "";
}
return addTempCode("<img style=\"max-width: 100%\" id=\"$imgId\" alt=\"image\" src=\"$src\"" .($enableImageResizer ? " onload=\"Scale(this,$image_max_width,$image_max_height);\" onclick=\"Preview(this);\"" : "") . " />");
return addTempCode("<img style=\"max-width: 100%\" id=\"$imgId\" alt=\"image\" src=\"$src\"" .
($enableImageResizer ?
" onload=\"Scale(this, $image_max_width, $image_max_height);\" onclick=\"Preview(this);\" " : "") .
" onerror=\"handleImageError(this, '$src');\" />");
}
function formatFlash($src, $width, $height) {

View File

@@ -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();
}

54
public/js/nexus.js vendored
View File

@@ -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