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

63 lines
4.6 KiB
JavaScript

// R68 验证:账号信息 / 视频信息属性内容宽度 180px
const CDP = 9223;
const list = await fetch(`http://localhost:${CDP}/json`).then((r) => r.json());
const page = list.find((t) => t.type === 'page' && (t.url || '').includes('8900')) || list.find((t) => t.type === 'page');
if (!page) { console.log('ERR: no page'); process.exit(1); }
const ws = new WebSocket(page.webSocketDebuggerUrl);
let id = 0; const pending = new Map();
const send = (method, params = {}) => new Promise((res, rej) => { const i = ++id; pending.set(i, { res, rej }); ws.send(JSON.stringify({ id: i, method, params })); });
ws.onmessage = (e) => { const m = JSON.parse(e.data); if (m.id && pending.has(m.id)) { const p = pending.get(m.id); pending.delete(m.id); m.error ? p.rej(new Error(m.error.message)) : p.res(m.result); } };
await new Promise((r) => (ws.onopen = r));
const ev = async (expr) => { const r = await send('Runtime.evaluate', { expression: expr, returnByValue: true, awaitPromise: true }); if (r.exceptionDetails) return { __err: (r.exceptionDetails.exception?.description || r.exceptionDetails.text).slice(0, 200) }; return r.result.value; };
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
await send('Page.enable'); await send('Runtime.enable'); await send('Network.enable');
await send('Network.setCacheDisabled', { cacheDisabled: true });
await send('Emulation.setDeviceMetricsOverride', { width: 900, height: 1100, deviceScaleFactor: 1, mobile: false });
const waitFor = async (expr, ms = 15000) => { const t0 = Date.now(); while (Date.now() - t0 < ms) { if (await ev(expr)) return true; await sleep(300); } return false; };
const fs = await import('node:fs');
const out = {};
// 1) 视频详情
await send('Page.navigate', { url: `http://127.0.0.1:8900/?t=${Date.now()}#/video/93` });
if (!(await waitFor(`location.hash === '#/video/93' && !!document.querySelector('#view .video-grid') && [...document.styleSheets].some((s) => (s.href || '').includes('style.css?v=20260904i'))`, 15000))) { console.log('FAIL video route'); process.exit(1); }
await sleep(500);
const vw = await ev(`(() => {
const items = [...document.querySelectorAll('.video-info .video-grid .info-item:not(.info-wide)')];
if (!items.length) return { err: 'no items' };
const widths = items.slice(0, 4).map((el) => Math.round(el.getBoundingClientRect().width));
const body = document.querySelector('.detail-body') || document.querySelector('main');
const over = items.some((el) => el.getBoundingClientRect().right > (body?.getBoundingClientRect().right || 9999));
return { widths, over, n: items.length };
})()`);
out.video = vw;
// 2) 账号详情(取第一个账号)
const accId = await ev(`fetch('/api/dsh/accounts?page=1&pageSize=1').then(r=>r.json()).then(d => d.items && d.items[0] ? d.items[0].id : null).catch(()=>null)`);
if (accId) {
await send('Page.navigate', { url: `http://127.0.0.1:8900/?t=${Date.now()}#/account/${accId}` });
if (!(await waitFor(`location.hash === '#/account/${accId}' && !!document.querySelector('.account-info .account-grid')`, 15000))) { console.log('FAIL account route'); process.exit(1); }
await sleep(500);
const aw = await ev(`(() => {
const items = [...document.querySelectorAll('.account-info .account-grid .info-item')];
if (!items.length) return { err: 'no items' };
const widths = items.slice(0, 4).map((el) => Math.round(el.getBoundingClientRect().width));
const body = document.querySelector('.detail-body') || document.querySelector('main');
const over = items.some((el) => el.getBoundingClientRect().right > (body?.getBoundingClientRect().right || 9999));
return { widths, over, n: items.length, acc: '#' + location.hash };
})()`);
out.account = aw;
// 截图账号信息卡
const rect = await ev(`(() => { const el = document.querySelector('.account-info'); if (!el) return null; const r = el.getBoundingClientRect(); return { x: r.x, y: r.y, w: r.width, h: r.height }; })()`);
if (rect) {
const shot = await send('Page.captureScreenshot', { format: 'png', clip: { x: Math.max(0, rect.x - 6), y: Math.max(0, rect.y - 6), width: Math.min(rect.w + 12, 1200), height: Math.min(rect.h + 12, 900), scale: 1 } });
fs.writeFileSync('tmp/r68_account_info.png', Buffer.from(shot.data, 'base64'));
}
} else { out.account = { err: 'no account in db' }; }
console.log(JSON.stringify(out, null, 1));
const ok = out.video && !out.video.err && out.video.widths.length && out.video.widths.every((w) => w === 180) && !out.video.over
&& out.account && !out.account.err && out.account.widths.length && out.account.widths.every((w) => w === 180) && !out.account.over;
console.log(ok ? '\n=== PASS ===' : '\n=== CHECK ===');
ws.close();
process.exit(ok ? 0 : 1);