From 64251d26b55d00a64aa24b7ff5c078733cf36b7a Mon Sep 17 00:00:00 2001 From: PetrichorFun <127877748+PetrichorFun@users.noreply.github.com> Date: Thu, 5 Mar 2026 15:40:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E9=A1=B5=E5=9B=BE=E7=89=87=E4=B8=8B=E8=BD=BD=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=8A=A0=E8=BD=BD=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E9=87=8D=E8=AF=95=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/page-worker/web-injectors/amazon.ts | 40 +++++++++++++++---------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/page-worker/web-injectors/amazon.ts b/src/page-worker/web-injectors/amazon.ts index e30f606..6e327fc 100644 --- a/src/page-worker/web-injectors/amazon.ts +++ b/src/page-worker/web-injectors/amazon.ts @@ -492,10 +492,9 @@ export class AmazonDetailPageInjector extends BaseInjector { const overlay = document.querySelector('.overlayRestOfImages'); if (overlay) { - // document.querySelector('#canvasCaption > a')?.click(); if (document.querySelector('#ivThumbs')!.getClientRects().length === 0) { + // Click to see full view document.querySelector('#canvasCaption > a')?.click(); - // overlay.click(); await new Promise((resolve) => setTimeout(resolve, 1000)); } } @@ -516,7 +515,6 @@ export class AmazonDetailPageInjector extends BaseInjector { ); } - // 替换原来的 forEach (第524-537行) if (urls.length === 0) { const divs: NodeListOf = document.querySelectorAll('.ivRow div.ivThumbImage'); @@ -526,24 +524,36 @@ export class AmazonDetailPageInjector extends BaseInjector { div.click(); // 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 - const imgDom = document.querySelector('#ivLargeImage > img'); + // 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 + let retryCount = 0; + const maxRetries = 5; - // get url from img dom src attribute - if (imgDom && 'src' in imgDom) { - urls.push(imgDom.src as string); + while (true) { + const loadingGifUrl = + '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('header > [data-action="a-popover-close"]')?.click(); - // close the image overlay if exists - document - .querySelector('#a-popover-4 > div > div:nth-child(1) > button') - ?.click(); - + document.querySelector('body > div.a-modal-scroller.a-declarative')?.click(); return urls; }); }