/** Wait until the fields required by the detail parser are available. */ export async function waitForAmazonDetailPageReady() { const deadline = Date.now() + 20000; const sleep = (milliseconds: number) => new Promise((resolve) => setTimeout(resolve, milliseconds)); while (Date.now() < deadline) { if (window.location.pathname.startsWith('/ap/signin')) { throw new Error('Amazon 详情页需要重新登录'); } if (document.querySelector('form[action*="validateCaptcha"], #captchacharacters')) { throw new Error('Amazon 详情页触发了验证码'); } const unavailable = document.querySelector( '#availability .a-color-price, #outOfStock, #exports_desktop_qualifiedBuybox', ); const title = document.querySelector('#title, #productTitle'); const titleReady = Boolean(title?.textContent?.trim()); if (document.readyState !== 'loading' && titleReady) { title?.scrollIntoView({ behavior: 'auto', block: 'center' }); return true; } if (document.readyState !== 'loading' && unavailable) { throw new Error('Amazon 商品当前不可用,详情标题未加载'); } await sleep(300); } throw new Error('Amazon 详情标题在 20 秒内未加载完成'); }