probe_baseline.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * 定点诊断:在**旧页面**的 fault 页签 DOM 里找 "前三" / "关键少数" 这类文本,打印其父元素 —— 弄清它们由谁产出。
  3. * 用法:node tools/probe_baseline.mjs fault 前三
  4. */
  5. import { JSDOM, VirtualConsole } from 'jsdom'
  6. const GATEWAY = 'http://127.0.0.1:28084'
  7. const TAB = process.argv[2] || 'fault'
  8. const NEEDLE = process.argv[3] || '前三'
  9. const html = await (await fetch(GATEWAY + '/detail/v2')).text()
  10. const dom = new JSDOM(html, {
  11. url: `${GATEWAY}/detail/v2#tab=${TAB}`, runScripts: 'dangerously', pretendToBeVisual: true,
  12. virtualConsole: new VirtualConsole(),
  13. beforeParse: (w) => {
  14. w.fetch = async (i, init) => {
  15. const raw = typeof i === 'string' ? i : i.url
  16. const u = raw.startsWith('http') ? raw : GATEWAY + (raw.startsWith('/') ? raw : '/' + raw)
  17. const r = await fetch(u, init)
  18. const b = await r.text()
  19. return { ok: r.ok, status: r.status, headers: { get: (k) => r.headers.get(k) }, json: async () => JSON.parse(b), text: async () => b }
  20. }
  21. },
  22. })
  23. const doc = dom.window.document
  24. for (let i = 0; i < 120; i++) {
  25. if (doc.querySelector('#root .page-head')) break
  26. await new Promise((r) => setTimeout(r, 500))
  27. }
  28. const walk = (n) => {
  29. for (const c of n.childNodes) {
  30. if (c.nodeType === 3 && c.textContent.includes(NEEDLE)) {
  31. const p = c.parentElement
  32. console.log(`文本: "${c.textContent.trim().slice(0, 60)}"`)
  33. console.log(` 父: <${p.tagName.toLowerCase()} class="${p.className}">`)
  34. console.log(` 父外框: ${p.outerHTML.slice(0, 300).replace(/\s+/g, ' ')}`)
  35. console.log('')
  36. } else if (c.nodeType === 1) walk(c)
  37. }
  38. }
  39. walk(doc.getElementById('root'))