-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.mjs
45 lines (38 loc) · 1.05 KB
/
screenshot.mjs
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
import puppeteer from 'puppeteer';
import fs from 'fs';
async function getBrowser() {
return await puppeteer.launch();
}
async function getPage(browser, url) {
const page = await browser.newPage();
await page.waitFor(500);
await page.goto(url.link);
return page;
}
async function setViewports(page, { width, height}) {
await page.setViewport({
width: width,
height: height,
});
}
async function saveScreenshot(page, device, url) {
const { name, width, height } = device;
const newLocation = `screenshots/${name}(${width}-${height})`;
fs.mkdir(newLocation, (err) => {});
await page.screenshot({
path: `${newLocation}/${url.name}.png`,
fullPage: true
});
}
export default async function getUrlAndResolutions(devices, urls) {
fs.mkdir('screenshots', (err) => {});
for (let url of urls) {
const browser = await getBrowser();
const page = await getPage(browser, url);
for (let device of devices) {
await setViewports(page, device);
await saveScreenshot(page, device, url)
}
browser.close();
}
}