50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
// 通过 CDP 检查页面详细内容
|
|
const wsUrl = process.argv[2] || "ws://127.0.0.1:9222/devtools/page/2";
|
|
|
|
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 {
|
|
const expr = `JSON.stringify({
|
|
bodyText: document.body.innerText.slice(0, 600),
|
|
loading: !!document.querySelector('.ant-spin, .loading, [class*=loading]'),
|
|
rootChildren: document.getElementById('root') ? document.getElementById('root').children.length : -1,
|
|
nav: (() => { const n = document.querySelector('nav'); return n ? n.innerText.slice(0,100) : 'NO_NAV'; })(),
|
|
header: (() => { const h = document.querySelector('header'); return h ? h.innerText.slice(0,100) : 'NO_HEADER'; })(),
|
|
imgs: document.images.length,
|
|
scripts: document.scripts.length
|
|
})`;
|
|
const res = await send("Runtime.evaluate", { expression: expr, returnByValue: true });
|
|
console.log(res.result.value);
|
|
} catch (e) {
|
|
console.error("Error:", e.message);
|
|
} finally {
|
|
ws.close();
|
|
process.exit(0);
|
|
}
|
|
}; |