-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot.js
More file actions
48 lines (44 loc) · 1.49 KB
/
Copy pathscreenshot.js
File metadata and controls
48 lines (44 loc) · 1.49 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
// Dual-viewport screenshot tool. Usage: node screenshot.js
const puppeteer = require('puppeteer');
const path = require('path');
const fileUrl = 'file:///' + path
.resolve(__dirname, 'index.html')
.replace(/\\/g, '/')
.replace(/ /g, '%20');
const VIEWPORTS = [
{ name: 'desktop', width: 1440, height: 900 },
{ name: 'mobile', width: 390, height: 844 },
];
async function revealAll(page) {
await page.evaluate(async () => {
document.querySelectorAll('.reveal').forEach((el) => el.classList.add('in'));
await new Promise((resolve) => {
let y = 0;
const step = () => {
y += window.innerHeight * 0.8;
window.scrollTo(0, y);
if (y < document.body.scrollHeight) setTimeout(step, 110);
else { window.scrollTo(0, 0); setTimeout(resolve, 400); }
};
step();
});
});
}
(async () => {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--enable-unsafe-swiftshader'],
});
for (const vp of VIEWPORTS) {
const page = await browser.newPage();
await page.setViewport({ width: vp.width, height: vp.height, deviceScaleFactor: 1 });
await page.goto(fileUrl, { waitUntil: 'networkidle2', timeout: 60000 });
await revealAll(page);
await new Promise((r) => setTimeout(r, 1200));
const out = path.resolve(__dirname, `screenshot-${vp.name}.png`);
await page.screenshot({ path: out, fullPage: true });
console.log('saved', out);
await page.close();
}
await browser.close();
})();