Files
mcn-short-video/project/短视频脚本创作/V1.0/mcn-work-shop/cdp-diag-videoinfo.mjs
T

61 lines
2.7 KiB
JavaScript

/* cdp-diag-videoinfo.mjs — 诊断视频详情渲染失败(09-01) */
const CDP = 'http://127.0.0.1:9223';
function cdp(ws, method, params = {}) {
return new Promise((resolve, reject) => {
const id = ++cdp._id;
const w = new WebSocket(ws);
const timer = setTimeout(() => { try { w.close(); } catch (e) {} reject(new Error('timeout: ' + method)); }, 30000);
w.onopen = () => w.send(JSON.stringify({ id, method, params }));
w.onmessage = (ev) => {
const m = JSON.parse(ev.data);
if (m.id !== id) return;
clearTimeout(timer);
try { w.close(); } catch (e) {}
m.error ? reject(new Error(method + ': ' + JSON.stringify(m.error))) : resolve(m.result);
};
w.onerror = () => { clearTimeout(timer); reject(new Error('ws error: ' + method)); };
});
}
cdp._id = 0;
async function evalJs(ws, expr) {
const r = await cdp(ws, 'Runtime.evaluate', { expression: expr, awaitPromise: true, returnByValue: true });
return r.result && r.result.value;
}
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function main() {
const tabs = await (await fetch(CDP + '/json')).json();
const tab = tabs.find((x) => x.url.includes('localhost:8900')) || tabs.find((x) => x.type === 'page');
const ws = tab.webSocketDebuggerUrl;
await cdp(ws, 'Page.enable');
await cdp(ws, 'Runtime.enable');
const errors = [];
const onEvt = (ev) => {
const m = JSON.parse(ev.data);
if (m.method === 'Runtime.exceptionThrown') errors.push(JSON.stringify(m.params.exceptionDetails).slice(0, 400));
if (m.method === 'Runtime.consoleAPICalled' && m.params.type === 'error') errors.push('[console.error] ' + (m.params.args || []).map((a) => a.value || a.description || '').join(' ').slice(0, 300));
};
const sock = new WebSocket((await (await fetch(CDP + '/json')).json()).find((x) => x.id === tab.id).webSocketDebuggerUrl);
sock.onmessage = onEvt;
await new Promise((r) => (sock.onopen = r));
await evalJs(ws, `location.href = 'http://localhost:8900/?v=diag1#/video/680'`);
await sleep(4000);
const state = await evalJs(ws, `(() => {
const title = document.querySelector('.video-detail-title');
const err = document.querySelector('.empty');
return {
hash: location.hash,
hasTitle: !!title,
emptyText: err ? err.textContent.trim().slice(0, 60) : '',
bodyStart: document.body.textContent.trim().slice(0, 200),
hasRenderFail: document.body.textContent.includes('渲染失败'),
hasAccReset: document.body.textContent.includes('accReset')
};
})()`);
console.log('STATE:', JSON.stringify(state, null, 2));
console.log('JS ERRORS:', errors.length ? errors.join('\n') : '无');
process.exit(0);
}
main().catch((e) => { console.error('ERROR:', e.message); process.exit(1); });