mirror of
https://github.com/primedigitaltech/azon_seeker.git
synced 2026-10-04 14:53:22 +08:00
fix: 优化详情页图片下载逻辑,添加加载等待重试机制
This commit is contained in:
parent
3befed6d76
commit
64251d26b5
@ -492,10 +492,9 @@ export class AmazonDetailPageInjector extends BaseInjector {
|
||||
const overlay = document.querySelector<HTMLElement>('.overlayRestOfImages');
|
||||
|
||||
if (overlay) {
|
||||
// document.querySelector<HTMLElement>('#canvasCaption > a')?.click();
|
||||
if (document.querySelector<HTMLElement>('#ivThumbs')!.getClientRects().length === 0) {
|
||||
// Click to see full view
|
||||
document.querySelector<HTMLElement>('#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<HTMLDivElement> =
|
||||
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<HTMLElement>('header > [data-action="a-popover-close"]')?.click();
|
||||
// close the image overlay if exists
|
||||
document
|
||||
.querySelector<HTMLElement>('#a-popover-4 > div > div:nth-child(1) > button')
|
||||
?.click();
|
||||
|
||||
document.querySelector<HTMLElement>('body > div.a-modal-scroller.a-declarative')?.click();
|
||||
return urls;
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user