Files

54 lines
2.7 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 = 9366;
const BASE = process.argv[2] || 'http://localhost:5173';
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() {
for (let i = 0; i < 40; i++) { try { if ((await (await fetch(`http://localhost:${PORT}/json/version`)).json()).webSocketDebuggerUrl) break; } catch {} await sleep(250); }
const PROJECTS = ['A7', 'O6', 'O6_new', 'O6_fingers'];
for (const id of PROJECTS) {
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('Runtime.enable');
await send('Page.navigate', { url: BASE + '/' });
await sleep(1500);
await send('Runtime.evaluate', { expression: `localStorage.setItem('assembly.activeProject','${id}')` });
await send('Page.navigate', { url: BASE + '/customize' });
await sleep(11000);
// 只保留 3D canvas,隐藏所有面板/头部,得到纯模型图(不受面板左右位置影响)
await send('Runtime.evaluate', {
expression: `(() => {
const s = document.createElement('style');
s.textContent = 'body *{visibility:hidden!important} canvas{visibility:visible!important}';
document.head.appendChild(s);
})()`,
});
await sleep(400);
const shot = await send('Page.captureScreenshot', { format: 'png' });
writeFileSync(`scripts/out/thumb-${id}.png`, Buffer.from(shot.data, 'base64'));
console.log('wrote scripts/out/thumb-' + id + '.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); });