forked from 11ty/11ty-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-screenshots.js
86 lines (74 loc) · 2.16 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const puppeteer = require("puppeteer");
const fastglob = require("fast-glob");
const eleventyImage = require("@11ty/eleventy-img");
// only generate superfeatured screenshots
const superFeaturedOnly = false;
const withJs = false;
const deviceName = 'iPad landscape';
const waitAfterLoad = 1000;
const eleventyImageOptions = {
formats: ["avif", "webp", "jpeg"],
widths: [300, 600], // 260-440 in layout
outputDir: "./img/sites/"
};
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.emulate(puppeteer.devices[deviceName]);
// await page.setViewport({
// width: 1024,
// height: 768,
// deviceScaleFactor: 1,
// });
// be nice, superfeatured ones go on the home page
// disable on foursquare because it has a captcha
if(!withJs) {
page.setJavaScriptEnabled(false);
}
await page.goto(url, {
waitUntil: ["load", "networkidle0"]
});
await pause(waitAfterLoad);
let buffer = await page.screenshot({
type: "jpeg",
quality: 100,
// encoding: "binary" // defaults to binary
});
let options = Object.assign({
sourceUrl: url,
filenameFormat: function(id, src, width, format) {
return `${fileSlug}-${width}${withJs ? "-js" : ""}.${format}`;
}
}, eleventyImageOptions);
await eleventyImage(buffer, options);
await browser.close();
}
(async () => {
let sites = await fastglob("./_data/sites/*.json", {
caseSensitiveMatch: false
});
let i = 0;
for(let site of sites) {
i++;
let siteData = require(site);
if(siteData.url && !siteData.disabled && (!superFeaturedOnly || siteData.superfeatured)) {
let filename = site.split("/").pop().replace(/\.json/, "");
console.log( `${i} of ${sites.length}`, "Fetching", siteData.url, "to", filename );
try {
await screenshot(siteData.url, filename);
} catch(e) {
console.log( ">>> Error:", e );
}
} else {
console.log( `${i} of ${sites.length}`, "Skipping", siteData.url );
}
}
})();