forked from 11ty/11ty-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-screenshots.js
50 lines (44 loc) · 1.28 KB
/
node-screenshots.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
const puppeteer = require("puppeteer");
const slugify = require("slugify");
const sharp = require("sharp");
const fastestSites = require("./_data/fastestSites.json");
async function pause(time) {
let p = new Promise(resolve => {
setTimeout(() => {
resolve();
}, time);
});
await p;
}
async function screenshot(url, fileSlug) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, {
waitUntil: ["load", "networkidle0"]
});
await pause(1000);
await page.setViewport({
width: 1024,
height: 768,
deviceScaleFactor: 1,
});
let fullFilename = `./img/sites/${fileSlug}_full.jpg`;
let filename = `./img/sites/${fileSlug}.jpg`;
await page.screenshot({
path: fullFilename,
type: "jpeg",
quality: 80
});
await sharp(fullFilename).resize(405).toFile(filename);
await browser.close();
}
(async () => {
for(let site of fastestSites) {
if(site.rank <= 11 || site.accessibilityRank <= 11 || (site.rank + site.accessibilityRank <= 100)) {
console.log( "Fetching", site.url, `Performance #${site.rank} Accessibility #${site.accessibilityRank}` );
let slug = site.url.replace(/https?\:\//, "");
let filename = slugify(slug, { lower: true, remove: /[:\/]/g });
await screenshot(site.url, filename);
}
}
})();