// node tools/schermate/schermata.mjs [larghezza] [altezza] [attesa-ms] [passo.js] // // Screenshot of Febe's own interface (spec §10.7) through the DevTools protocol of a Chromium without window: // exact viewport at scale 1.25 (like the mockups), console errors printed, an optional step (a JS file evaluated in // the page and awaited; when it returns { hover: [x, y] } the pointer is moved there, so :hover styles show). // // Only for Febe's own interface: the address must be on this computer (127.0.0.1 or localhost). Instagram is never // opened this way (spec §2, rule 4). Needs Node 22 or later (global WebSocket) and a Chromium: set FEBE_CHROMIUM, or // the Playwright build under /opt/pw-browsers is used. import { spawn } from 'node:child_process'; import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; const [address, out, width = '1440', height = '900', wait = '3000', step] = process.argv.slice(2); if (!address || !out) { console.error('Uso: node tools/schermate/schermata.mjs [larghezza] [altezza] [attesa-ms] [passo.js]'); process.exit(2); } const target = new URL(address); if (!['127.0.0.1', 'localhost', '[::1]'].includes(target.hostname)) { console.error('Solo l\'interfaccia di Febe su questo computer (127.0.0.1 o localhost).'); process.exit(2); } function findChromium() { if (process.env.FEBE_CHROMIUM) return process.env.FEBE_CHROMIUM; const root = '/opt/pw-browsers'; if (existsSync(root)) { for (const name of readdirSync(root).filter((n) => /^chromium-\d+$/.test(n)).sort().reverse()) { const candidate = join(root, name, 'chrome-linux', 'chrome'); if (existsSync(candidate)) return candidate; } } console.error('Chromium non trovato: indica il percorso in FEBE_CHROMIUM.'); process.exit(2); } const profile = mkdtempSync(join(tmpdir(), 'febe-schermata-')); const port = 9300 + Math.floor(Math.random() * 500); const chrome = spawn(findChromium(), [ '--headless', '--no-sandbox', '--disable-gpu', '--hide-scrollbars', '--disable-background-networking', '--disable-component-update', '--no-first-run', '--no-default-browser-check', '--disable-extensions', `--user-data-dir=${profile}`, `--remote-debugging-port=${port}`, '--remote-debugging-address=127.0.0.1', 'about:blank', ], { stdio: 'ignore' }); const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); function finish(code) { chrome.kill('SIGKILL'); setTimeout(() => { try { rmSync(profile, { recursive: true, force: true }); } catch { // the profile stays in the temporary folder } process.exit(code); }, 300); } let ws; for (let i = 0; i < 50 && !ws; i++) { try { const list = await (await fetch(`http://127.0.0.1:${port}/json/list`)).json(); const page = list.find((t) => t.type === 'page'); if (page) ws = new WebSocket(page.webSocketDebuggerUrl); } catch { // Chromium is still starting } if (!ws) await sleep(200); } if (!ws) { console.error('Chromium non risponde.'); finish(1); } else { await new Promise((r) => ws.addEventListener('open', r)); let id = 0; const pending = new Map(); ws.addEventListener('message', (e) => { const msg = JSON.parse(e.data); if (msg.id && pending.has(msg.id)) { pending.get(msg.id)(msg); pending.delete(msg.id); } else if (msg.method === 'Runtime.consoleAPICalled' && ['error', 'warning'].includes(msg.params.type)) { console.log(`console.${msg.params.type}:`, msg.params.args.map((a) => a.value ?? a.description).join(' ')); } else if (msg.method === 'Runtime.exceptionThrown') { console.log('eccezione:', msg.params.exceptionDetails.exception?.description || msg.params.exceptionDetails.text); } else if (msg.method === 'Log.entryAdded' && msg.params.entry.level === 'error') { console.log('errore:', msg.params.entry.text, msg.params.entry.url || ''); } }); const send = (method, params = {}) => new Promise((resolve) => { const n = ++id; pending.set(n, resolve); ws.send(JSON.stringify({ id: n, method, params })); }); await send('Runtime.enable'); await send('Log.enable'); await send('Page.enable'); await send('Emulation.setDeviceMetricsOverride', { width: Number(width), height: Number(height), deviceScaleFactor: 1.25, mobile: false }); await send('Page.navigate', { url: target.href }); await sleep(Number(wait)); if (step) { const code = readFileSync(step, 'utf8'); const r = await send('Runtime.evaluate', { expression: `(async () => { ${code} })()`, awaitPromise: true, returnByValue: true }); const value = r.result?.result?.value; if (r.result?.exceptionDetails) console.log('errore nel passo:', r.result.exceptionDetails.exception?.description); else if (value !== undefined) console.log('passo:', JSON.stringify(value)); if (value && typeof value === 'object' && Array.isArray(value.hover)) { await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: value.hover[0], y: value.hover[1] }); } await sleep(800); } const shot = await send('Page.captureScreenshot', { format: 'png' }); writeFileSync(out, Buffer.from(shot.result.data, 'base64')); console.log('salvata', out); ws.close(); finish(0); }