Files
image-suite-studio/extension/src/collector/scan.ts
T

64 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 统一采集引擎入口 - 只做路由:按平台分发到 platforms/ 下的平台文件。
*
* 平台编排逻辑(各路径与合并策略)见:
* platforms/ozon.ts Ozon 四路径(SSR data-state / JSON-LD / 站内 API / DOM
* platforms/taobao.ts 淘宝/天猫(桥读全局 SSR / DOM)
* platforms/1688.ts 1688(桥读 window.context SSR / DOM
* 共用合并器见 merge.ts。
*/
import { matchProfile } from '../profiles';
import { readWindowKeys } from '../bridge/read-window';
import { scanOzon } from './platforms/ozon';
import { scanTaobao } from './platforms/taobao';
import { scan1688 } from './platforms/1688';
import type { ScanResult } from './merge';
export type { ScanResult };
export type { ImageMaterial, TextMaterial } from './merge';
export async function scanCurrentPage(): Promise<ScanResult | null> {
const profile = matchProfile(location.href);
if (!profile) {
console.warn('[SuiteCollector] 当前页面不支持采集:', location.href);
return null;
}
const itemId = profile.extractItemId(location.href);
console.log('[SuiteCollector] 开始采集:', profile.name, itemId, location.href);
let result: ScanResult | null = null;
try {
if (profile.id === 'ozon') result = await scanOzon(profile, itemId);
else if (profile.id === 'taobao') result = await scanTaobao(profile, itemId);
else result = await scan1688(profile, itemId);
} catch (err) {
console.error('[SuiteCollector] 采集异常:', err);
return null;
}
console.log('[SuiteCollector] 采集完成:', {
platform: result.platform,
texts: result.texts.map((t) => t.kind),
images: result.images.length,
stats: result.stats,
warnings: result.warnings,
source: result.source,
});
return result;
}
// 诊断工具:列出页面全局数据键(在商品页 console 跑 __SuiteCollector.probe()
export async function probeWindowKeys(): Promise<string[]> {
const res = await readWindowKeys(['*'], 1500);
return (res['__sc_window_keys__'] as string[]) ?? [];
}
// 暴露到全局供 side panel / console 调用
if (typeof window !== 'undefined') {
(window as any).__SuiteCollector = {
scan: scanCurrentPage,
probe: probeWindowKeys,
};
}