-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (74 loc) · 2.03 KB
/
index.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
87
88
89
90
91
const path = require('path');
const puppeteer = require(process.cwd() + "/node_modules/puppeteer");
// Support for pkg
const executablePath =
process.env.PUPPETEER_EXECUTABLE_PATH ||
(process.pkg
? path.join(
path.dirname(process.execPath),
'puppeteer',
...puppeteer
.executablePath()
.split(path.sep)
.slice(6), // /snapshot/project/node_modules/puppeteer/.local-chromium
)
: puppeteer.executablePath());
if(process.argv.length < 3){
console.log("You need to supply the URL as parameter: flyscreen <URL>");
process.exit(0);
}
(async () => {
// 1. Launch the browser
const browser = await puppeteer.launch({executablePath, defaultViewport: null});
// 2. Open a new page
const page = await browser.newPage();
try {
await page.setViewport({
width: 400,
height: 2000,
deviceScaleFactor: 1,
// isLandscape: true
});
for (let i = 2; i < process.argv.length; i++) {
await takeScreen(page, process.argv[i]);
}
await page.setViewport({
width: 800,
height: 2000,
deviceScaleFactor: 1,
});
for (let i = 2; i < process.argv.length; i++) {
await takeScreen(page, process.argv[i]);
}
await page.setViewport({
width: 1024,
height: 2000,
deviceScaleFactor: 1,
});
for (let i = 2; i < process.argv.length; i++) {
await takeScreen(page, process.argv[i]);
}
await page.setViewport({
width: 2048,
height: 2000,
deviceScaleFactor: 1,
});
for (let i = 2; i < process.argv.length; i++) {
await takeScreen(page, process.argv[i]);
}
await browser.close();
process.exit(0);
} catch (e) {
console.log(e);
process.exit(1);
}
})();
async function takeScreen(page, url){
// 3. Navigate to URL
await page.goto(url);
let resolution = page.viewport().width + 'x' + page.viewport().height;
console.log(`Resolution: ${resolution}`);
let path = 'screenshots/' + `screenshot-${url}`.replace(/\//g,".") + resolution + '.png';
// 4. Take screenshot
await page.screenshot({path: path});
}