42 lines
2.2 KiB
JavaScript
42 lines
2.2 KiB
JavaScript
import { spawn } from 'node:child_process';
|
|
import { writeFileSync } from 'node:fs';
|
|
import { homedir } from 'node:os';
|
|
import { resolve } from 'node:path';
|
|
|
|
const CHROME = resolve(
|
|
homedir(),
|
|
'Library/Caches/ms-playwright/chromium_headless_shell-1208/chrome-headless-shell-mac-arm64/chrome-headless-shell',
|
|
);
|
|
const PORT = 9355;
|
|
const BASE = process.argv[2] || 'http://localhost:5174';
|
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
const chrome = spawn(CHROME, [
|
|
`--remote-debugging-port=${PORT}`, '--headless', '--disable-gpu', '--no-sandbox',
|
|
'--enable-unsafe-swiftshader', '--use-gl=angle', '--use-angle=swiftshader',
|
|
'--window-size=1440,940', '--hide-scrollbars', '--force-device-scale-factor=2', 'about:blank',
|
|
]);
|
|
chrome.stderr.on('data', () => {});
|
|
let ws, msgId = 0; const pending = new Map();
|
|
const send = (method, params = {}) => { const id = ++msgId; ws.send(JSON.stringify({ id, method, params })); return new Promise((res) => pending.set(id, res)); };
|
|
|
|
async function main() {
|
|
let url;
|
|
for (let i = 0; i < 40; i++) { try { url = (await (await fetch(`http://localhost:${PORT}/json/version`)).json()).webSocketDebuggerUrl; if (url) break; } catch {} await sleep(250); }
|
|
const routes = [['home', '/'], ['workbench', '/workbench'], ['demo', '/demo']];
|
|
for (const [name, path] of routes) {
|
|
const tab = await (await fetch(`http://localhost:${PORT}/json/new?about:blank`, { method: 'PUT' })).json();
|
|
ws = new WebSocket(tab.webSocketDebuggerUrl);
|
|
await new Promise((res, rej) => { ws.onopen = res; ws.onerror = rej; });
|
|
ws.onmessage = (e) => { const m = JSON.parse(e.data); if (m.id && pending.has(m.id)) { pending.get(m.id)(m.result); pending.delete(m.id); } };
|
|
await send('Page.enable');
|
|
await send('Page.navigate', { url: BASE + path });
|
|
await sleep(name === 'workbench' ? 9000 : 3500);
|
|
const shot = await send('Page.captureScreenshot', { format: 'png' });
|
|
writeFileSync(`scripts/out/ui-${name}.png`, Buffer.from(shot.data, 'base64'));
|
|
console.log('wrote scripts/out/ui-' + name + '.png');
|
|
await send('Target.closeTarget', { targetId: tab.id }).catch(() => {});
|
|
}
|
|
chrome.kill(); process.exit(0);
|
|
}
|
|
main().catch((e) => { console.error(e); chrome.kill(); process.exit(1); });
|