48 lines
2.3 KiB
JavaScript
48 lines
2.3 KiB
JavaScript
// 采样检查卡片打字机是否真的在逐字打出
|
|||
|
|
import { spawn } from 'node:child_process';
|
||
|
|
import { mkdtempSync } from 'node:fs';
|
||
|
|
import { tmpdir } from 'node:os';
|
||
|
|
import { join } from 'node:path';
|
||
|
|
|
||
|
|
const CHROME = 'C:/Users/QunLIn/.agent-browser/browsers/chrome-154.0.8037.92/chrome.exe';
|
||
|
|
const PORT = 9344;
|
||
|
|
const URL_ = process.argv[2] || 'http://127.0.0.1:1313/comment.html';
|
||
|
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||
|
|
const userDir = mkdtempSync(join(tmpdir(), 'cdp-type-'));
|
||
|
|
const chrome = spawn(CHROME, ['--headless=new', `--remote-debugging-port=${PORT}`, `--user-data-dir=${userDir}`,
|
||
|
|
'--no-first-run', '--disable-gpu', '--window-size=1280,900', 'about:blank'], { stdio: 'ignore' });
|
||
|
|
|
||
|
|
const PROBE = `(() => {
|
||
|
|
const el = document.querySelector('.atk-fl-typed');
|
||
|
|
const caret = document.querySelector('.atk-fl-caret');
|
||
|
|
return JSON.stringify({
|
||
|
|
reduce: window.matchMedia('(prefers-reduced-motion: reduce)').matches,
|
||
|
|
len: el ? (el.textContent || '').length : -1,
|
||
|
|
caretShown: caret ? getComputedStyle(caret).display !== 'none' : null,
|
||
|
|
cardConnected: !!(document.querySelector('.atk-fl-card') && document.querySelector('.atk-fl-card').isConnected),
|
||
|
|
});
|
||
|
|
})()`;
|
||
|
|
|
||
|
|
try {
|
||
|
|
for (let i = 0; i < 40; i++) {
|
||
|
|
try { const r = await fetch(`http://127.0.0.1:${PORT}/json/version`); if (r.ok) break; } catch {}
|
||
|
|
await sleep(300);
|
||
|
|
}
|
||
|
|
const tab = await (await fetch(`http://127.0.0.1:${PORT}/json/new?${encodeURIComponent(URL_)}`, { method: 'PUT' })).json();
|
||
|
|
const ws = new WebSocket(tab.webSocketDebuggerUrl);
|
||
|
|
let id = 0; const pending = new Map();
|
||
|
|
ws.addEventListener('message', (ev) => { const m = JSON.parse(ev.data); if (m.id && pending.has(m.id)) { pending.get(m.id)(m); pending.delete(m.id); } });
|
||
|
|
await new Promise((res) => ws.addEventListener('open', res));
|
||
|
|
const send = (method, params = {}) => new Promise((res) => { const mid = ++id; pending.set(mid, res); ws.send(JSON.stringify({ id: mid, method, params })); });
|
||
|
|
await send('Runtime.enable');
|
||
|
|
for (let t = 0; t < 26; t++) {
|
||
|
|
const r = await send('Runtime.evaluate', { expression: PROBE, returnByValue: true });
|
||
|
|
console.log(String(t * 200).padStart(5) + 'ms', r.result && r.result.result ? r.result.result.value : r);
|
||
|
|
await sleep(200);
|
||
|
|
}
|
||
|
|
ws.close();
|
||
|
|
} catch (e) {
|
||
|
|
console.error('ERROR', e.message);
|
||
|
|
} finally {
|
||
|
|
chrome.kill();
|
||
|
|
}
|