-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·52 lines (48 loc) · 1.26 KB
/
app.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
const puppeteer = require("puppeteer");
const fs = require("fs");
const parseArgs = require("minimist");
const _ = require("lodash");
/**
* takeScreenShot
*
* @param {string} urlParam
* @param {int} width
* @param {int} height
*/
const takeScreenShot = async (
urlParam = "https://example.com",
width = 1920,
height = 1080
) => {
let url = new URL(_.trim(urlParam, "/"));
const browser = await puppeteer.launch({
headless: true,
args: [
"--disable-gpu",
"--disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--no-sandbox",
],
});
const page = await browser.newPage();
await page.setViewport({
width: width,
height: height,
});
await page.goto(url);
let imageName = url.pathname.split("/").slice(-1)[0];
let imagePath = url.pathname.replace(imageName, "");
imageName = imageName ? imageName : url.host;
let finalImagePath = `/media/${imagePath}${imageName}.gen.webp`;
await fs.mkdir(`/media/${imagePath}`, { recursive: true }, () => {});
await page.screenshot({ path: finalImagePath });
await browser.close();
};
let args = parseArgs(process.argv.splice(2));
takeScreenShot(args.url, args.width, args.height)
.then(() => {
console.log("Done.");
})
.catch((e) => {
console.error(e);
});