55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
// 检查路由后的页面状态和控制台错误
|
|
const wsUrl = process.argv[2] || "ws://127.0.0.1:9222/devtools/page/3";
|
|
|
|
const ws = new WebSocket(wsUrl);
|
|
let id = 0;
|
|
const pending = new Map();
|
|
|
|
function send(method, params = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const msgId = ++id;
|
|
pending.set(msgId, { resolve, reject });
|
|
ws.send(JSON.stringify({ id: msgId, method, params }));
|
|
});
|
|
}
|
|
|
|
ws.onmessage = (event) => {
|
|
const msg = JSON.parse(event.data);
|
|
if (msg.id && pending.has(msg.id)) {
|
|
const { resolve, reject } = pending.get(msg.id);
|
|
pending.delete(msg.id);
|
|
if (msg.error) reject(new Error(JSON.stringify(msg.error)));
|
|
else resolve(msg.result);
|
|
}
|
|
};
|
|
|
|
ws.onerror = (e) => {
|
|
console.error("WS error:", e.message || e);
|
|
process.exit(1);
|
|
};
|
|
|
|
ws.onopen = async () => {
|
|
try {
|
|
// 启用控制台日志
|
|
await send("Runtime.enable");
|
|
await send("Log.enable");
|
|
|
|
// 等待页面稳定
|
|
await new Promise(r => setTimeout(r, 3000));
|
|
|
|
const expr = `JSON.stringify({
|
|
url: location.href,
|
|
bodyText: document.body.innerText.slice(0, 500),
|
|
overflowX: document.documentElement.scrollWidth > document.documentElement.clientWidth,
|
|
viewport: { w: window.innerWidth, h: window.innerHeight },
|
|
scrollH: document.documentElement.scrollHeight
|
|
})`;
|
|
const res = await send("Runtime.evaluate", { expression: expr, returnByValue: true });
|
|
console.log("PAGE_STATE:", res.result.value);
|
|
} catch (e) {
|
|
console.error("Error:", e.message);
|
|
} finally {
|
|
ws.close();
|
|
process.exit(0);
|
|
}
|
|
}; |