| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // 无头浏览器验证:加载页面,抓 console 错误与页面渲染结果
- const CHROME = "/tmp/pwbrowsers/chromium_headless_shell-1255/chrome-headless-shell-linux64/chrome-headless-shell";
- const puppeteerPath = "/opt/data/vendor-build/node_modules";
- const { spawn } = require("child_process");
- const http = require("http");
- const fs = require("fs");
- const URL = process.argv[2] || "http://127.0.0.1:8588/index.html";
- // 用 CDP 直连,不依赖 puppeteer
- const net = require("net");
- const port = 9333 + Math.floor(Math.random() * 200);
- const chrome = spawn(CHROME, [
- `--remote-debugging-port=${port}`,
- "--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage",
- "--headless", "--window-size=1440,900",
- "about:blank"
- ], { stdio: ["ignore", "pipe", "pipe"] });
- let chromeErr = "";
- chrome.stderr.on("data", d => { chromeErr += d.toString(); });
- function httpGet(path) {
- return new Promise((res, rej) => {
- const req = http.get({ host: "127.0.0.1", port, path }, r => {
- let b = ""; r.on("data", c => b += c); r.on("end", () => res(JSON.parse(b)));
- });
- req.on("error", rej);
- req.setTimeout(15000, () => { req.destroy(); rej(new Error("timeout")); });
- });
- }
- (async () => {
- // 等 chrome 起来
- let targets = null;
- for (let i = 0; i < 40; i++) {
- try { targets = await httpGet("/json/list"); break; } catch (e) { await new Promise(r => setTimeout(r, 250)); }
- }
- if (!targets) { console.log("CHROME_FAIL\n" + chromeErr.slice(0, 800)); chrome.kill(); return; }
- const ws = targets.find(t => t.type === "page");
- if (!ws) { console.log("NO_PAGE"); chrome.kill(); return; }
- const WebSocket = globalThis.WebSocket;
- const sock = new WebSocket(ws.webSocketDebuggerUrl, { maxPayload: 200 * 1024 * 1024 });
- let id = 0; const pending = new Map(); const logs = []; const errors = [];
- sock.on("message", raw => {
- const m = JSON.parse(raw);
- if (m.id && pending.has(m.id)) { pending.get(m.id)(m); pending.delete(m.id); return; }
- if (m.method === "Runtime.consoleAPICalled") {
- const txt = (m.params.args || []).map(a => a.value ?? a.description ?? a.type).join(" ");
- logs.push(`[${m.params.type}] ${txt}`);
- }
- if (m.method === "Runtime.exceptionThrown") {
- const d = m.params.exceptionDetails;
- errors.push(d.exception?.description || d.text);
- }
- if (m.method === "Log.entryAdded" && m.params.entry.level === "error") {
- errors.push(m.params.entry.text + " " + (m.params.entry.url || ""));
- }
- });
- const send = (method, params = {}) => new Promise(res => {
- const i = ++id; pending.set(i, res);
- sock.send(JSON.stringify({ id: i, method, params }));
- });
- await new Promise(r => sock.on("open", r));
- await send("Runtime.enable");
- await send("Log.enable");
- await send("Page.enable");
- await send("Page.navigate", { url: URL });
- await new Promise(r => setTimeout(r, 5000));
- const r1 = await send("Runtime.evaluate", {
- expression: `JSON.stringify({
- title: document.title,
- bodyLen: document.body.innerText.length,
- text: document.body.innerText.slice(0,600),
- hasCustomEl: !!document.querySelector('hw-app, product-gallery, [data-app]'),
- rootHTML: (document.querySelector('#app')?.innerHTML || '').slice(0,400),
- babylon: typeof BABYLON !== 'undefined' ? BABYLON.Engine?.Version : 'MISSING'
- })`, returnByValue: true
- });
- console.log("=== PAGE STATE ===");
- console.log(r1.result?.result?.value || JSON.stringify(r1).slice(0, 500));
- console.log("=== CONSOLE (" + logs.length + ") ===");
- console.log(logs.slice(-25).join("\n"));
- console.log("=== ERRORS (" + errors.length + ") ===");
- console.log(errors.slice(0, 12).join("\n---\n"));
- sock.close(); chrome.kill();
- })().catch(e => { console.log("FATAL", e.message); chrome.kill(); });
|