From 935cac3c618a8c0f666fc37c34dabf05b5e03901 Mon Sep 17 00:00:00 2001 From: ywkj Date: Mon, 30 Mar 2026 14:05:53 +0800 Subject: [PATCH] feat: extract window.context.result.data for structured logistics data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Poll window.context.result.data (up to 15s) for productPackInfo, productTitle, productAttributes, and skuSelection. This provides structured weight/size/volume data per-variant directly from 1688's JS context — more reliable than vision-only extraction. Screenshots still captured as fallback for data only in images. Co-Authored-By: Claude Opus 4.6 --- src/index.ts | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) 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, };