verify.cjs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // 无头浏览器验证:加载页面,抓 console 错误与页面渲染结果
  2. const CHROME = "/tmp/pwbrowsers/chromium_headless_shell-1255/chrome-headless-shell-linux64/chrome-headless-shell";
  3. const puppeteerPath = "/opt/data/vendor-build/node_modules";
  4. const { spawn } = require("child_process");
  5. const http = require("http");
  6. const fs = require("fs");
  7. const URL = process.argv[2] || "http://127.0.0.1:8588/index.html";
  8. // 用 CDP 直连,不依赖 puppeteer
  9. const net = require("net");
  10. const port = 9333 + Math.floor(Math.random() * 200);
  11. const chrome = spawn(CHROME, [
  12. `--remote-debugging-port=${port}`,
  13. "--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage",
  14. "--headless", "--window-size=1440,900",
  15. "about:blank"
  16. ], { stdio: ["ignore", "pipe", "pipe"] });
  17. let chromeErr = "";
  18. chrome.stderr.on("data", d => { chromeErr += d.toString(); });
  19. function httpGet(path) {
  20. return new Promise((res, rej) => {
  21. const req = http.get({ host: "127.0.0.1", port, path }, r => {
  22. let b = ""; r.on("data", c => b += c); r.on("end", () => res(JSON.parse(b)));
  23. });
  24. req.on("error", rej);
  25. req.setTimeout(15000, () => { req.destroy(); rej(new Error("timeout")); });
  26. });
  27. }
  28. (async () => {
  29. // 等 chrome 起来
  30. let targets = null;
  31. for (let i = 0; i < 40; i++) {
  32. try { targets = await httpGet("/json/list"); break; } catch (e) { await new Promise(r => setTimeout(r, 250)); }
  33. }
  34. if (!targets) { console.log("CHROME_FAIL\n" + chromeErr.slice(0, 800)); chrome.kill(); return; }
  35. const ws = targets.find(t => t.type === "page");
  36. if (!ws) { console.log("NO_PAGE"); chrome.kill(); return; }
  37. const WebSocket = globalThis.WebSocket;
  38. const sock = new WebSocket(ws.webSocketDebuggerUrl, { maxPayload: 200 * 1024 * 1024 });
  39. let id = 0; const pending = new Map(); const logs = []; const errors = [];
  40. sock.on("message", raw => {
  41. const m = JSON.parse(raw);
  42. if (m.id && pending.has(m.id)) { pending.get(m.id)(m); pending.delete(m.id); return; }
  43. if (m.method === "Runtime.consoleAPICalled") {
  44. const txt = (m.params.args || []).map(a => a.value ?? a.description ?? a.type).join(" ");
  45. logs.push(`[${m.params.type}] ${txt}`);
  46. }
  47. if (m.method === "Runtime.exceptionThrown") {
  48. const d = m.params.exceptionDetails;
  49. errors.push(d.exception?.description || d.text);
  50. }
  51. if (m.method === "Log.entryAdded" && m.params.entry.level === "error") {
  52. errors.push(m.params.entry.text + " " + (m.params.entry.url || ""));
  53. }
  54. });
  55. const send = (method, params = {}) => new Promise(res => {
  56. const i = ++id; pending.set(i, res);
  57. sock.send(JSON.stringify({ id: i, method, params }));
  58. });
  59. await new Promise(r => sock.on("open", r));
  60. await send("Runtime.enable");
  61. await send("Log.enable");
  62. await send("Page.enable");
  63. await send("Page.navigate", { url: URL });
  64. await new Promise(r => setTimeout(r, 5000));
  65. const r1 = await send("Runtime.evaluate", {
  66. expression: `JSON.stringify({
  67. title: document.title,
  68. bodyLen: document.body.innerText.length,
  69. text: document.body.innerText.slice(0,600),
  70. hasCustomEl: !!document.querySelector('hw-app, product-gallery, [data-app]'),
  71. rootHTML: (document.querySelector('#app')?.innerHTML || '').slice(0,400),
  72. babylon: typeof BABYLON !== 'undefined' ? BABYLON.Engine?.Version : 'MISSING'
  73. })`, returnByValue: true
  74. });
  75. console.log("=== PAGE STATE ===");
  76. console.log(r1.result?.result?.value || JSON.stringify(r1).slice(0, 500));
  77. console.log("=== CONSOLE (" + logs.length + ") ===");
  78. console.log(logs.slice(-25).join("\n"));
  79. console.log("=== ERRORS (" + errors.length + ") ===");
  80. console.log(errors.slice(0, 12).join("\n---\n"));
  81. sock.close(); chrome.kill();
  82. })().catch(e => { console.log("FATAL", e.message); chrome.kill(); });