// scripts/render-local-html.js — playwright 渲染本地 HTML 文件,提取 DOM 文本 const { chromium } = require('playwright'); const fs = require('fs'); const os = require('os'); const path = require('path'); (async () => { const filePath = process.argv[2]; const outPath = process.argv[3] || path.join(os.tmpdir(), path.basename(filePath).replace(/\.html$/, '-dom.txt')); const url = 'file:///' + filePath.replace(/\\/g, '/'); const browser = await chromium.launch({ headless: true }); const page = await browser.newPage(); // 等动态内容(阿里云 SPA 的 React 渲染可能稍慢) await page.goto(url, { waitUntil: 'networkidle', timeout: 60000 }).catch(() => {}); for (let i = 0; i < 6; i++) { await page.waitForTimeout(2000); const tables = await page.locator('table').count(); console.log(` attempt ${i + 1}: tables=${tables}`); if (tables >= 2) break; } const text = await page.evaluate(() => { document.querySelectorAll('script,style,noscript').forEach(e => e.remove()); return document.body.innerText; }); fs.writeFileSync(outPath, text, 'utf8'); console.log(`dom text: ${text.length} bytes → ${outPath}`); console.log(`first 300 chars:`); console.log(text.slice(0, 300)); await browser.close(); })().catch(e => { console.error(e); process.exit(1); });