-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture-screenshots.js
More file actions
65 lines (56 loc) · 2.32 KB
/
Copy pathcapture-screenshots.js
File metadata and controls
65 lines (56 loc) · 2.32 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
const puppeteer = require('puppeteer');
const path = require('path');
const fs = require('fs');
const ROOT = path.join(__dirname, '..');
const IMAGES_DIR = path.join(ROOT, 'images');
const projects = JSON.parse(fs.readFileSync(path.join(ROOT, 'projects.json'), 'utf8'));
async function captureScreenshots() {
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const status = {
generatedAt: new Date().toISOString(),
projects: {},
};
for (const project of projects) {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800, deviceScaleFactor: 1.5 });
// Pin dark. Every site is dark-only now, so this is belt-and-braces — but CI reports
// no OS preference (which resolves to light), so it keeps captures deterministic here
// and on GitHub Actions, and guards any future project that ships a light theme.
await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'dark' }]);
try {
console.log(`Capturing: ${project.id} (${project.url})`);
const started = Date.now();
const response = await page.goto(project.url, { waitUntil: 'networkidle0', timeout: 30000 });
const responseMs = Date.now() - started;
await new Promise((r) => setTimeout(r, 2000));
const outputPath = path.join(IMAGES_DIR, `${project.id}.webp`);
await page.screenshot({ path: outputPath, type: 'webp', quality: 78, fullPage: false });
status.projects[project.id] = {
up: response !== null && response.ok(),
httpStatus: response ? response.status() : null,
responseMs,
capturedAt: new Date().toISOString(),
};
console.log(` Saved: ${outputPath} (${status.projects[project.id].httpStatus}, ${responseMs}ms)`);
} catch (err) {
console.error(` Error capturing ${project.id}: ${err.message}`);
status.projects[project.id] = {
up: false,
httpStatus: null,
responseMs: null,
capturedAt: new Date().toISOString(),
error: err.message,
};
} finally {
await page.close();
}
}
await browser.close();
fs.writeFileSync(path.join(ROOT, 'status.json'), JSON.stringify(status, null, 2) + '\n');
console.log('Wrote status.json');
console.log('Done.');
}
captureScreenshots();