-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.js
69 lines (59 loc) · 1.69 KB
/
screenshot.js
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
const puppeteer = require('puppeteer-core');
const fs = require('fs');
const urls = fs.readFileSync('urls.txt').toString().split("\n");
const sizes = [
{ name: 'iphone',
width: 375,
height: 667
},
{
name: 'desktop',
width: 1440,
height: 900
}
]
let count = 1;
async function getUrlSizes(devices, urls) {
for (let url of urls) {
for (let size of sizes) {
console.log(`${size.name}: ${url}`)
try{
await getScreenShot(url, size);
}catch(e){
await console.error(`Error with ${url}`, e);
}
}
count++
}
}
getUrlSizes(sizes, urls);
async function getScreenShot(url, size){
const browser = await puppeteer.launch({
executablePath: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome',
headless: true
});
const page = await browser.newPage();
await page.setViewport({
width: size.width,
height: size.height,
deviceScaleFactor: 2,
});
await page.goto(url, {waitUntil: 'networkidle2'}); // can also try networkidle0
// Delay for page to lazy load stuff - combine this with "headless: false" to click away modals
await page.waitForTimeout(2000);
// For full page length screenshot, reset browser height to page height
// OR use fullPage option below
// const bodyHeight = await page.evaluate(() => document.body.scrollHeight);
// await page.setViewport({
// width: size.width,
// height: bodyHeight,
// deviceScaleFactor: 2
// })
const thisURL = new URL(page.url()).hostname;
const fileName = `${thisURL}-${size.width}_${count.toString().padStart(2, '0')}`;
await page.screenshot({
path: `export/${fileName}.png`,
fullPage: true
});
await browser.close();
}