diff --git a/src/index.ts b/src/index.ts index bb9dd30..1079cc0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,8 @@ export interface ScrapeResult { command: Command; dryRun: boolean; offerId: string; + productPackInfo?: unknown; + windowContext?: unknown; screenshots?: string[]; detailImages?: string[]; error?: string; @@ -202,8 +204,37 @@ export async function run( await cdp.send('Page.navigate', { url }); - // Wait for page load + dynamic content - await new Promise(r => setTimeout(r, 5000)); + // Wait for window.context.result.data to be populated (poll up to 15s) + let productPackInfo: unknown = null; + let windowContext: unknown = null; + for (let i = 0; i < 30; i++) { + await new Promise(r => setTimeout(r, 500)); + const ctx = await cdp.evaluate(` + (function() { + try { + const d = window.context && window.context.result && window.context.result.data; + if (d && d.productPackInfo) { + return JSON.stringify({ + productPackInfo: d.productPackInfo, + productTitle: d.productTitle || null, + productAttributes: d.productAttributes || null, + skuSelection: d.skuSelection || null, + }); + } + } catch(e) {} + return null; + })() + `); + if (ctx) { + const parsed = JSON.parse(ctx); + productPackInfo = parsed.productPackInfo; + windowContext = parsed; + break; + } + } + + // Extra wait for remaining dynamic content (images etc) + await new Promise(r => setTimeout(r, 2000)); const outputDir = path.join('/tmp', '1688-logistics', offerId); @@ -217,6 +248,8 @@ export async function run( return { status: 'success', url, command, dryRun, offerId, + productPackInfo, + windowContext, screenshots, detailImages, };