Repository navigation
Expand file tree
/
Copy pathscreenshots.mjs
More file actions
94 lines (80 loc) · 2.51 KB
/
Copy pathscreenshots.mjs
File metadata and controls
94 lines (80 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Regenerates the README screenshots from the running HELIOS instance.
//
// Usage: HELIOS_ADMIN_PASSWORD=… bun run screenshots [url]
import { chromium } from 'playwright';
import { spawn } from 'node:child_process';
const BASE_URL = process.argv[2] ?? 'http://raspi.fritz.box:3999';
const VIEWPORT = { width: 1280, height: 800 };
const SCALE = 2;
const PAGES = [
{
file: 'screenshot-configuration.png',
path: '/sensors',
waitFor: 'text=INVERTER',
},
{
file: 'screenshot-services.png',
path: '/services',
waitFor: 'text=Dashboard',
},
{
file: 'screenshot-backup.png',
path: '/backups',
// The page paints an empty shell first and lazy-loads this frame.
waitFor: 'turbo-frame#backups-content section',
},
];
const password = process.env.HELIOS_ADMIN_PASSWORD;
if (!password) {
console.error('HELIOS_ADMIN_PASSWORD is not set');
process.exit(1);
}
// Playwright writes an unoptimized 24-bit PNG. Quantizing it to a 256-color
// palette halves the file size, with no visible loss on flat UI surfaces.
// Needs ImageMagick, see the Brewfile.
function writeOptimized(buffer, file) {
return new Promise((resolve, reject) => {
const magick = spawn(
'magick',
[
'png:-',
'-strip',
'-colors',
'256',
'-define',
'png:compression-level=9',
`PNG8:${file}`,
],
{ stdio: ['pipe', 'inherit', 'inherit'] },
);
magick.on('error', reject);
magick.on('close', (code) =>
code === 0 ? resolve() : reject(new Error(`magick exited with ${code}`)),
);
magick.stdin.end(buffer);
});
}
const browser = await chromium.launch();
// A fresh context carries no preferences cookie, so the locale comes from
// Accept-Language and the screenshots stay English.
const context = await browser.newContext({
viewport: VIEWPORT,
deviceScaleFactor: SCALE,
locale: 'en-US',
});
const page = await context.newPage();
await page.goto(`${BASE_URL}/session/new`);
await page.fill('#password', password);
await page.click('button[type=submit]');
await page.waitForURL(/\/services/);
for (const { file, path, waitFor } of PAGES) {
await page.goto(BASE_URL + path);
await page.waitForSelector(waitFor);
await page.waitForLoadState('networkidle');
// Live values (host stats, sensor readings, container health) arrive over
// ActionCable after the initial render.
await page.waitForTimeout(3000);
await writeOptimized(await page.screenshot(), file);
console.log(`Wrote ${file}`);
}
await browser.close();