82 lines
3.2 KiB
JavaScript
82 lines
3.2 KiB
JavaScript
/* Print console messages + page errors for one URL in headless Edge. */
|
|
const { spawn } = require("node:child_process");
|
|
const fs = require("node:fs");
|
|
const http = require("node:http");
|
|
|
|
const EDGE = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
|
|
const PORT = 9334;
|
|
const url = process.argv[2] || "http://localhost:3210/";
|
|
const cookieFile = process.argv[3];
|
|
|
|
function httpJson(path) {
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request({ host: "127.0.0.1", port: PORT, path }, (res) => {
|
|
let d = "";
|
|
res.on("data", (c) => (d += c));
|
|
res.on("end", () => { try { resolve(JSON.parse(d)); } catch { resolve(d); } });
|
|
});
|
|
req.on("error", reject);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
(async () => {
|
|
const profile = fs.mkdtempSync("/tmp/edge-prof-");
|
|
const proc = spawn(EDGE, [
|
|
"--headless=new", `--remote-debugging-port=${PORT}`, `--user-data-dir=${profile}`,
|
|
"--no-first-run", "--no-default-browser-check", "--disable-gpu", "about:blank",
|
|
], { stdio: "ignore" });
|
|
|
|
let targets = null;
|
|
for (let i = 0; i < 40; i++) {
|
|
await new Promise((r) => setTimeout(r, 500));
|
|
try { targets = await httpJson("/json/list"); break; } catch {}
|
|
}
|
|
if (!targets) { console.error("edge did not start"); process.exit(1); }
|
|
const page = targets.find((t) => t.type === "page");
|
|
|
|
const ws = new WebSocket(page.webSocketDebuggerUrl);
|
|
await new Promise((r) => (ws.onopen = r));
|
|
let id = 0;
|
|
const send = (method, params = {}) =>
|
|
new Promise((resolve) => {
|
|
const myId = ++id;
|
|
ws.send(JSON.stringify({ id: myId, method, params }));
|
|
const t = setInterval(() => {
|
|
const m = ws._buf.find((b) => b.id === myId);
|
|
if (m) { clearInterval(t); resolve(m.result ?? m.error); }
|
|
}, 10);
|
|
});
|
|
ws._buf = [];
|
|
ws.onmessage = (m) => {
|
|
const msg = JSON.parse(m.data);
|
|
if (msg.id) { ws._buf.push(msg); return; }
|
|
if (msg.method === "Runtime.consoleAPICalled") {
|
|
const text = msg.params.args.map((a) => a.value ?? a.description ?? a.type).join(" ");
|
|
console.log(`[${msg.params.type}] ${text}`.slice(0, 3000));
|
|
}
|
|
if (msg.method === "Runtime.exceptionThrown") {
|
|
console.log("[EXCEPTION]", JSON.stringify(msg.params.exceptionDetails.exception?.description ?? msg.params.exceptionDetails.text).slice(0, 3000));
|
|
}
|
|
if (msg.method === "Log.entryAdded") {
|
|
const e = msg.params.entry;
|
|
console.log(`[log:${e.level}] ${e.text}`.slice(0, 3000));
|
|
}
|
|
};
|
|
|
|
await send("Runtime.enable");
|
|
await send("Log.enable");
|
|
await send("Network.enable");
|
|
if (cookieFile) {
|
|
const line = fs.readFileSync(cookieFile, "utf8").split("\n").map((l) => l.trim()).find((l) => l.includes("authjs.session-token"));
|
|
if (line) await send("Network.setCookies", { cookies: [{ name: "authjs.session-token", value: line.split("\t").pop(), domain: "localhost", path: "/", httpOnly: true, sameSite: "Lax" }] });
|
|
}
|
|
await send("Page.enable");
|
|
await send("Page.navigate", { url });
|
|
await new Promise((r) => setTimeout(r, 4500));
|
|
const cls = await send("Runtime.evaluate", { expression: "document.documentElement.className" });
|
|
console.log("ROOT CLASS:", cls?.result?.value);
|
|
proc.kill();
|
|
process.exit(0);
|
|
})().catch((e) => { console.error(e.message); process.exit(1); });
|