-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.js
55 lines (46 loc) · 1.19 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
const puppeteer = require('puppeteer');
const sharp = require('sharp');
const takeScreenshot = async (url) => {
const browser = await puppeteer.launch({
args: [ '--no-sandbox' ]
});
const page = await browser.newPage();
await page.goto(url, {
waitUntil: 'networkidle0'
});
const height = await page.evaluate(() => {
const body = document.body,
html = document.documentElement;
return Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
});
await page.setViewport({ width: 1920, height });
const screenshot = await page.screenshot({
type: 'jpeg'
});
cleanedUrl = url
.replace(/\./g, '')
.replace(/\//g, '')
.replace(/\:/g, '')
.slice(0, 20);
const imagePath = `img/${cleanedUrl}${Math.floor(Math.random() * 100)}.jpg`;
sharp(screenshot)
.resize(Math.floor(1920 / 1.5), Math.floor(height / 1.5))
.toFile(imagePath);
await browser.close();
return imagePath;
};
(async () => {
try {
const url = process.argv[2];
const res = await takeScreenshot(url);
console.log(res);
} catch (error) {
console.log(error.message);
}
})();