import { readFileSync } from 'node:fs' import { fileURLToPath, pathToFileURL } from 'node:url' import { dirname, resolve } from 'node:path' import { JSDOM, VirtualConsole } from 'jsdom' const HERE = dirname(fileURLToPath(import.meta.url)) const GATEWAY = process.argv[2] const CLASSIC = resolve(HERE, '../../../release/web/detail_v2.html') const state = { apis: {}, cache: new Map() } function installFetch(window, state) { window.fetch = async (input) => { const raw = typeof input === 'string' ? input : (input && input.url) || String(input) const url = raw.startsWith('http') ? raw : GATEWAY + (raw.startsWith('/') ? raw : '/' + raw) if (state.cache.has(url)) { const c = state.cache.get(url); return { ok: true, status: 200, headers: new Map(), text: async () => c, json: async () => JSON.parse(c) } } const r = await fetch(url); const b = await r.text(); if (r.ok) state.cache.set(url, b) return { ok: r.ok, status: r.status, headers: r.headers, text: async () => b, json: async () => JSON.parse(b) } } } const dom = new JSDOM(readFileSync(CLASSIC, 'utf8'), { url: GATEWAY + '/detail/v2#tab=component', runScripts: 'dangerously', pretendToBeVisual: true, virtualConsole: new VirtualConsole(), beforeParse: (w) => installFetch(w, state) }) const doc = dom.window.document for (let i = 0; i < 120; i++) { const r = doc.getElementById('root'); if (r && r.querySelector('.page-head')) break; await new Promise((x) => setTimeout(x, 500)) } let last = -1, same = 0 for (let i = 0; i < 40; i++) { const r = doc.getElementById('root'); const n = r ? r.querySelectorAll('*').length : 0; if (n === last) { if (++same >= 3) break } else same = 0; last = n; await new Promise((x) => setTimeout(x, 700)) } const root = doc.getElementById('root') // 基线:找第 240 个元素的父级上下文 const all = Array.from(root.querySelectorAll('*')) console.log('BASELINE_TOTAL', all.length) for (let i = 236; i < 246 && i < all.length; i++) { const el = all[i] console.log('B#' + i, el.tagName.toLowerCase() + (el.className ? '.' + String(el.className).trim().split(/\s+/).join('.') : ''), '|parent=' + (el.parentElement ? el.parentElement.tagName.toLowerCase() + '.' + String(el.parentElement.className || '').trim().split(/\s+/).join('.') : '-')) } const entry = pathToFileURL(resolve(HERE, '../target/ssr/entry-server.js')).href const html = await (await import(entry)).renderTab(state.cache.get(GATEWAY + '/api/fleet') ? JSON.parse(state.cache.get(GATEWAY + '/api/fleet')) : null, 'component', null, null, null, {}) const vd = new JSDOM('