fix: 优化详情页图片下载逻辑,添加加载等待重试机制

This commit is contained in:
PetrichorFun 2026-03-05 15:40:43 +08:00
parent 3befed6d76
commit 64251d26b5

View File

@ -492,10 +492,9 @@ export class AmazonDetailPageInjector extends BaseInjector {
const overlay = document.querySelector<HTMLElement>('.overlayRestOfImages'); const overlay = document.querySelector<HTMLElement>('.overlayRestOfImages');
if (overlay) { if (overlay) {
// document.querySelector<HTMLElement>('#canvasCaption > a')?.click();
if (document.querySelector<HTMLElement>('#ivThumbs')!.getClientRects().length === 0) { if (document.querySelector<HTMLElement>('#ivThumbs')!.getClientRects().length === 0) {
// Click to see full view
document.querySelector<HTMLElement>('#canvasCaption > a')?.click(); document.querySelector<HTMLElement>('#canvasCaption > a')?.click();
// overlay.click();
await new Promise((resolve) => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
} }
} }
@ -516,7 +515,6 @@ export class AmazonDetailPageInjector extends BaseInjector {
); );
} }
// 替换原来的 forEach (第524-537行)
if (urls.length === 0) { if (urls.length === 0) {
const divs: NodeListOf<HTMLDivElement> = const divs: NodeListOf<HTMLDivElement> =
document.querySelectorAll('.ivRow div.ivThumbImage'); document.querySelectorAll('.ivRow div.ivThumbImage');
@ -526,24 +524,36 @@ export class AmazonDetailPageInjector extends BaseInjector {
div.click(); div.click();
// wait for image to load // wait for image to load
await new Promise((resolve) => setTimeout(resolve, 100 + ~~(500 * Math.random()))); await new Promise((resolve) => setTimeout(resolve, 500 + ~~(500 * Math.random())));
// get img dom // if img src is not loading gif, then get url and break, otherwise continue to wait, with a max retry count to avoid infinite loop
const imgDom = document.querySelector('#ivLargeImage > img'); let retryCount = 0;
const maxRetries = 5;
// get url from img dom src attribute while (true) {
if (imgDom && 'src' in imgDom) { const loadingGifUrl =
urls.push(imgDom.src as string); 'https://m.media-amazon.com/images/G/01/ui/loadIndicators/loading-large_labeled._CB485921664_.gif';
// get img dom
const imgDom = document.querySelector('#ivLargeImage > img');
// get url from img dom src attribute
if (imgDom && 'src' in imgDom && imgDom.src !== loadingGifUrl) {
urls.push(imgDom.src as string);
break;
} else {
// wait for 500ms and try again
await new Promise((resolve) => setTimeout(resolve, 500 + ~~(500 * Math.random())));
retryCount++;
if (retryCount >= maxRetries) {
console.error('在detail页面下载图片,已达最大重试次数:');
break;
}
}
} }
} }
} }
// document.querySelector<HTMLElement>('header > [data-action="a-popover-close"]')?.click(); document.querySelector<HTMLElement>('body > div.a-modal-scroller.a-declarative')?.click();
// close the image overlay if exists
document
.querySelector<HTMLElement>('#a-popover-4 > div > div:nth-child(1) > button')
?.click();
return urls; return urls;
}); });
} }