_check_shadow.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { readFileSync } from 'node:fs'
  2. import { fileURLToPath } from 'node:url'
  3. import { dirname, resolve } from 'node:path'
  4. import { JSDOM, VirtualConsole } from 'jsdom'
  5. const HERE = dirname(fileURLToPath(import.meta.url))
  6. const dist = resolve(HERE, '../../../release/web')
  7. // ① 门户导航
  8. let shell = readFileSync(dist + '/portal/shell.html', 'utf8')
  9. const ids = Array.from(shell.matchAll(/<!--@TEMPLATE:([^>]+?)-->/g)).map((m) => m[1])
  10. for (const id of ids) shell = shell.replace('<!--@TEMPLATE:' + id + '-->', '<template id="' + id + '"></template>')
  11. for (const id2 of ['cms', 'documents', 'recalc']) shell = shell.replace(new RegExp('<a[^>]*data-nav="' + id2 + '"[^>]*>[^<]*</a>', 'g'), '')
  12. console.log('NAV_CMS ' + /data-nav="cms"/.test(shell) + ' NAV_DOCS ' + /data-nav="documents"/.test(shell) + ' NAV_RECALC ' + /data-nav="recalc"/.test(shell))
  13. // ② 振动 CMS(Shadow DOM)
  14. const idx = readFileSync(dist + '/index.html', 'utf8')
  15. const jsFile = (idx.match(/src="([^"]+\.js)"/) || [])[1].replace(/^.*\//, '')
  16. const cssFile = (idx.match(/href="([^"]+\.css)"/) || [])[1].replace(/^.*\//, '')
  17. const code = readFileSync(dist + '/assets/' + jsFile, 'utf8')
  18. const css = readFileSync(dist + '/assets/' + cssFile, 'utf8')
  19. const fleet = readFileSync(resolve(HERE, '_fleet_fixture.json'), 'utf8')
  20. const vib = readFileSync(resolve(HERE, '_vibcms_fixture.json'), 'utf8')
  21. const dom = new JSDOM(idx.replace('</head>', '<style>' + css + '</style></head>'),
  22. { url: 'http://127.0.0.1:28084/detail/v2#tab=vibration:portal_cms', runScripts: 'outside-only', pretendToBeVisual: true, virtualConsole: new VirtualConsole() })
  23. const w = dom.window
  24. w.fetch = async (u) => { const s = String(u); const body = /fleet/.test(s) ? fleet : (/vibcms/.test(s) ? vib : '{}')
  25. return { ok: true, status: 200, json: async () => JSON.parse(body), text: async () => body } }
  26. try { w.eval(code) } catch (e) { console.log('EVAL_ERR ' + String(e).slice(0, 140)) }
  27. for (let i = 0; i < 20; i++) { await new Promise((r) => setTimeout(r, 400)); if (w.document.querySelector('[data-fsub-host]')) break }
  28. const host = w.document.querySelector('[data-fsub-host]')
  29. const sr = host && host.shadowRoot
  30. const inner = sr ? sr.innerHTML : ''
  31. console.log('HOST ' + (host ? 'Y' : 'N') + ' SHADOW ' + (sr ? 'Y' : 'N') + ' LEN ' + inner.length)
  32. console.log('HAS_PORTAL_CSS ' + /--ground|\.hero/.test(inner))
  33. console.log('HAS_HERO_TITLE ' + /振动与 SCADA 联合分析/.test(inner))
  34. console.log('HAS_CHIPS ' + /<div class="chips">/.test(inner) + ' CHIP_COUNT ' + (inner.match(/class="chip/g) || []).length)
  35. console.log('HAS_LIVE_WIN ' + /当前时间窗 · /.test(inner))