-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot.mjs
More file actions
55 lines (51 loc) · 1.99 KB
/
Copy pathscreenshot.mjs
File metadata and controls
55 lines (51 loc) · 1.99 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
import puppeteer from 'puppeteer-core';
import { mkdir, readdir } from 'node:fs/promises';
import { join } from 'node:path';
// Cached Chrome shipped with this machine's puppeteer install.
const CHROME = 'C:/Users/manim/.cache/puppeteer/chrome/win64-144.0.7559.96/chrome-win64/chrome.exe';
const OUT_DIR = join(process.cwd(), 'temporary screenshots');
const url = process.argv[2] || 'http://localhost:3000';
const label = process.argv[3] || '';
// Optional viewport: "mobile" -> 390x844, default desktop 1440x900
const mode = process.argv[4] || 'desktop';
const viewport = mode === 'mobile'
? { width: 390, height: 844, deviceScaleFactor: 2, isMobile: true }
: { width: 1440, height: 900, deviceScaleFactor: 1 };
async function nextName() {
await mkdir(OUT_DIR, { recursive: true });
const files = await readdir(OUT_DIR).catch(() => []);
let max = 0;
for (const f of files) {
const m = f.match(/^screenshot-(\d+)/);
if (m) max = Math.max(max, parseInt(m[1], 10));
}
const n = max + 1;
return join(OUT_DIR, `screenshot-${n}${label ? '-' + label : ''}.png`);
}
const browser = await puppeteer.launch({
executablePath: CHROME,
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
try {
const page = await browser.newPage();
await page.setViewport(viewport);
await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 });
// Scroll through the page to trigger IntersectionObserver reveals, then return to top.
await page.evaluate(async () => {
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const h = document.body.scrollHeight;
for (let y = 0; y <= h; y += Math.round(window.innerHeight * 0.7)) {
window.scrollTo(0, y);
await sleep(120);
}
window.scrollTo(0, 0);
await sleep(200);
});
await new Promise((r) => setTimeout(r, 800)); // let fonts/animations settle
const out = await nextName();
await page.screenshot({ path: out, fullPage: true });
console.log('Saved', out);
} finally {
await browser.close();
}