Files
chandra/scripts/screenshot.mjs
T
2026-11-24 23:51:26 +00:00

55 lines
2.9 KiB
JavaScript

#!/usr/bin/env node
// Drive the browser host with headless Chromium and save PNG screenshots.
// CHROME_PATH=/path/to/chrome node scripts/screenshot.mjs <outdir> [--tag name]
// Uses playwright-core (devDependency of host/) with an already-installed
// Chromium; nothing is downloaded here.
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { serve } from '../host/serve.mjs';
const require = createRequire(join(fileURLToPath(import.meta.url), '..', '..', 'host', 'package.json'));
const { chromium } = require('playwright-core');
const outdir = process.argv[2];
if (!outdir) { console.error('usage: screenshot.mjs <outdir> [--tag name]'); process.exit(2); }
const tagI = process.argv.indexOf('--tag');
const tag = tagI > 0 ? process.argv[tagI + 1] : 'desktop';
const exe = process.env.CHROME_PATH;
if (!exe) { console.error('error: set CHROME_PATH to a Chromium/Chrome executable'); process.exit(2); }
await mkdir(outdir, { recursive: true });
const srv = await serve(0);
const url = `http://127.0.0.1:${srv.address().port}/host/web/index.html`;
const browser = await chromium.launch({ executablePath: exe, args: ['--no-sandbox'] });
const page = await browser.newPage({ viewport: { width: 1280, height: 748 } });
const errors = [];
page.on('pageerror', (e) => errors.push(String(e)));
page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text()); });
const T = 'time=2026-11-24T14:07:37';
const shot = async (name, query, steps) => {
await page.goto(`${url}?${T}&${query}`);
await page.waitForFunction(() => window.moonPlasma?.ready || window.moonPlasmaError, null, { timeout: 30000 });
const err = await page.evaluate(() => window.moonPlasmaError);
if (err) throw new Error(`host failed in browser: ${err}`);
if (steps) await steps();
const f = join(outdir, `${tag}-${name}.png`);
await page.locator('#screen').screenshot({ path: f });
console.log(f);
};
const canvasPoint = async (x, y) => { const b = await page.locator('#screen').boundingBox(); return [b.x + x, b.y + y]; };
const click = async (x, y) => { const [px, py] = await canvasPoint(x, y); await page.mouse.move(px, py); await page.mouse.down(); await page.mouse.up(); };
await shot('1-desktop', 'config=desktop.json');
await shot('2-launcher-open', 'config=desktop.json', async () => {
await click(60, 700);
await page.keyboard.type('s');
const [px, py] = await canvasPoint(100, 450); await page.mouse.move(px, py);
});
await shot('3-binary-clock-swapped', 'config=desktop.json', async () => {
await page.evaluate(() => window.moonPlasma.swap('clock', 'clock-binary'));
});
await shot('4-day-theme', 'config=desktop.json&theme=moon-day', async () => { await click(60, 700); });
await browser.close(); srv.close();
if (errors.length) { console.error('browser errors:\n' + errors.join('\n')); process.exit(1); }