Files
blog/blog-admin/tools/check-hr-gap.mjs
T

71 lines
3.3 KiB
JavaScript
Raw Normal View History

// 量错误卡片与下方 <hr> 的真实几何关系
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 = 9395;
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-hr-'));
const chrome = spawn(CHROME, [
'--headless=new', `--remote-debugging-port=${PORT}`, `--user-data-dir=${userDir}`,
'--no-first-run', '--disable-gpu', '--window-size=1280,2000', 'about:blank',
], { stdio: 'ignore' });
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 })); });
const ev = async (expr) => {
const r = await send('Runtime.evaluate', { expression: expr, returnByValue: true, awaitPromise: true });
return r.result?.result?.value ?? r.result?.value ?? null;
};
await send('Runtime.enable');
await send('Page.enable');
await sleep(9000);
console.log(await ev(`(() => {
const layer = document.querySelector('.atk-error-layer');
const card = document.querySelector('.atk-err-card');
const btn = document.querySelector('.atk-err-retry');
if (!card || !btn) return 'NO CARD';
const r = (el) => { const b = el.getBoundingClientRect(); return { top: Math.round(b.top + scrollY), bottom: Math.round(b.bottom + scrollY), h: Math.round(b.height) }; };
// 找卡片之后最近的 hr(含主题里的)
let hr = null, el = layer;
while (el && !hr) {
let s = el.nextElementSibling;
while (s) { if (s.tagName === 'HR') { hr = s; break; } s = s.nextElementSibling; }
el = el.parentElement;
}
const lr = r(layer), br = r(btn), cr = r(card);
const hrr = hr ? r(hr) : null;
const cs = getComputedStyle(layer);
return JSON.stringify({
layer: lr, card: cr, button: br,
hr: hrr,
hrInsideLayer: hr ? layer.contains(hr) : null,
layerMargin: cs.margin,
gapBtnToHr: (hrr && br) ? hrr.top - br.bottom : null,
overlap: (hrr && br) ? (br.bottom > hrr.top) : null,
layerParentCls: layer.parentElement ? (layer.parentElement.className || '').slice(0, 60) : null,
nextSiblings: (() => { const out = []; let s = layer.nextElementSibling; for (let i = 0; s && i < 4; i++) { out.push(s.tagName + '.' + String(s.className).slice(0, 30)); s = s.nextElementSibling; } return out; })(),
}, null, 1);
})()`));
ws.close();
} catch (e) {
console.error('ERROR', e.message);
} finally {
chrome.kill();
}