| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /**
- * 诊断:为什么 jsdom 里跑旧 app.js 没把 DOM 渲出来(基线为空 ⇒ 对拍无从比)。
- * 打印:页面里的 script 标签形态、eval 是否抛错、渲染后 #root/#tabs 的内容抽样。
- */
- import { JSDOM, VirtualConsole } from 'jsdom'
- const GATEWAY = process.argv[2] || 'http://127.0.0.1:28084'
- const html = await (await fetch(GATEWAY + '/detail/v2')).text()
- const vc = new VirtualConsole()
- vc.on('jsdomError', (e) => console.log(' [jsdomError]', String(e.message).slice(0, 120)))
- const dom = new JSDOM(html, { url: GATEWAY + '/detail/v2', runScripts: 'dangerously', pretendToBeVisual: true, virtualConsole: vc })
- const { window } = dom
- const doc = window.document
- console.log('== 页面脚本标签 ==')
- for (const s of Array.from(doc.querySelectorAll('script'))) {
- const src = s.getAttribute('src')
- console.log(` type=${s.getAttribute('type') || 'classic'} src=${src || '(内联, ' + s.textContent.length + ' 字节)'}`)
- }
- console.log('== 容器初始状态 ==')
- for (const id of ['root', 'tabs', 'top', 'foot']) {
- const el = doc.getElementById(id)
- console.log(` #${id}: ${el ? (el.innerHTML.trim().length + ' 字节') : '不存在'}`)
- }
- // 装 fetch 桩(记录 fleet 响应)
- const state = {}
- window.fetch = async (input, init) => {
- const raw = typeof input === 'string' ? input : (input && input.url) || String(input)
- const url = raw.startsWith('http') ? raw : GATEWAY + (raw.startsWith('/') ? raw : '/' + raw)
- const resp = await fetch(url, init)
- const body = await resp.text()
- if (url.includes('/api/fleet')) state.fleet = body.length
- return { ok: resp.ok, status: resp.status, headers: { get: (k) => resp.headers.get(k) },
- json: async () => JSON.parse(body), text: async () => body }
- }
- console.log('== 手动执行外链脚本 ==')
- for (const s of Array.from(doc.querySelectorAll('script[src]'))) {
- const src = s.getAttribute('src')
- const code = await (await fetch(GATEWAY + src)).text()
- try {
- window.eval(code)
- console.log(` [OK] eval ${src}(${code.length} 字节)`)
- } catch (e) {
- console.log(` [X ] eval ${src} 抛错: ${e.name}: ${String(e.message).slice(0, 160)}`)
- }
- }
- for (let i = 0; i < 20; i++) {
- if (doc.querySelector('.kpis .kpi')) break
- await new Promise((r) => setTimeout(r, 500))
- }
- console.log('== 12 秒后 ==')
- console.log(' .kpis .kpi:', doc.querySelectorAll('.kpis .kpi').length)
- console.log(' #tabs 按钮:', doc.querySelectorAll('#tabs button').length)
- console.log(' button 总数:', doc.querySelectorAll('button').length)
- console.log(' #root 前 260 字:', (doc.getElementById('root')?.innerHTML || '').trim().slice(0, 260).replace(/\s+/g, ' '))
- console.log(' fleet 响应字节:', state.fleet ?? '(未请求)')
- console.log(' window.__WIN:', JSON.stringify(window.__WIN ?? null).slice(0, 80))
- console.log(' window.__L 键数:', window.__L ? Object.keys(window.__L).length : '(无)')
|