67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
// 检查学习页面的 API 请求和错误
|
|
const wsUrl = process.argv[2] || "ws://127.0.0.1:9222/devtools/page/12";
|
|
|
|
const ws = new WebSocket(wsUrl);
|
|
let id = 0;
|
|
const pending = new Map();
|
|
const errors = [];
|
|
|
|
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);
|
|
} else if (msg.method === "Runtime.exceptionThrown") {
|
|
errors.push("EXCEPTION: " + JSON.stringify(msg.params.exceptionDetails));
|
|
} else if (msg.method === "Log.entryAdded") {
|
|
errors.push("LOG: " + msg.params.entry.level + " " + msg.params.entry.text);
|
|
} else if (msg.method === "Network.responseReceived") {
|
|
const r = msg.params.response;
|
|
if (r.status >= 400) {
|
|
errors.push(`HTTP ${r.status}: ${r.url}`);
|
|
}
|
|
}
|
|
};
|
|
|
|
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 send("Network.enable");
|
|
|
|
// 重新加载学习页面
|
|
await send("Page.navigate", { url: "http://192.168.5.7/learn" });
|
|
await new Promise(r => setTimeout(r, 5000));
|
|
|
|
const expr = `JSON.stringify({
|
|
url: location.href,
|
|
bodyText: document.body.innerText.slice(0, 300),
|
|
courseCount: (() => { const m = document.body.innerText.match(/(\d+)门课程/); return m ? m[1] : 'N/A'; })()
|
|
})`;
|
|
const res = await send("Runtime.evaluate", { expression: expr, returnByValue: true });
|
|
console.log("PAGE:", res.result.value);
|
|
console.log("\nERRORS:");
|
|
errors.forEach(e => console.log(e));
|
|
if (errors.length === 0) console.log("(no errors captured)");
|
|
} catch (e) {
|
|
console.error("Error:", e.message);
|
|
} finally {
|
|
ws.close();
|
|
process.exit(0);
|
|
}
|
|
}; |