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