_check_center.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import { readFileSync } from 'node:fs'
  2. import { fileURLToPath, pathToFileURL } 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 GATEWAY = process.argv[2]
  7. const dist = resolve(HERE, '../../../release/web')
  8. const idx = readFileSync(dist + '/index.html', 'utf8')
  9. const jsFile = (idx.match(/src="\.\/assets\/([^"]+\.js)"/) || [])[1]
  10. const cssFile = (idx.match(/href="\.\/assets\/([^"]+\.css)"/) || [])[1]
  11. const code = readFileSync(dist + '/assets/' + jsFile, 'utf8')
  12. const css = readFileSync(dist + '/assets/' + cssFile, 'utf8')
  13. const dom = new JSDOM(idx.replace('</head>', '<style>' + css + '</style></head>'),
  14. { url: GATEWAY + '/web/index.html#workbench', runScripts: 'outside-only', pretendToBeVisual: true, virtualConsole: new VirtualConsole() })
  15. const w = dom.window
  16. w.fetch = async () => ({ ok: true, status: 200, json: async () => ({ win: '2026年', tabs: {} }), text: async () => '{}' })
  17. try { w.eval(code) } catch (e) { console.log('EVAL_ERR ' + String(e).slice(0, 140)) }
  18. await new Promise((r) => setTimeout(r, 1200))
  19. const d = w.document
  20. function probe(sel) {
  21. const el = d.querySelector(sel)
  22. if (!el) return sel + ' = 缺失'
  23. const cs = w.getComputedStyle(el)
  24. return sel + ' → max-width=' + (cs.maxWidth || '-') + ' marginLeft=' + (cs.marginLeft || '-')
  25. + ' marginRight=' + (cs.marginRight || '-') + ' padding=' + (cs.padding || '-') + ' width=' + (cs.width || '-')
  26. }
  27. for (const s of ['body', '#app', '.wrap', 'main#root', 'header.top', 'nav.tabs', 'footer#foot']) console.log(probe(s))
  28. // 是否存在把 .wrap 关掉的规则(旧 CSS 里有一条 .wrap{padding:0;max-width:none} 在媒体查询里)
  29. const m = css.match(/\.wrap\s*\{[^}]*max-width:none[^}]*\}/g)
  30. console.log('CSS_WRAP_OVERRIDE ' + (m ? JSON.stringify(m).slice(0, 200) : 'none'))
  31. const mq = css.match(/@media[^{]*\{[^@]*\.wrap\s*\{[^}]*\}/)
  32. console.log('CSS_WRAP_IN_MEDIA ' + (mq ? mq[0].slice(0, 220).replace(/\s+/g, ' ') : 'none'))