-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.js
99 lines (88 loc) · 3.33 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
92
93
94
95
96
97
98
99
#!/usr/bin/env node
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const program = require('commander');
program
.option('--url, [url]', 'The url')
.option('--emulate, [emulate]', 'emulate device')
.option('--fullpage, [fullpage]', 'Full Page')
.option('--pdf, [pdf]', 'Generate PDF')
.option('--w, [w]', 'width')
.option('--h, [h]', 'height')
.option('--waitfor, [waitfor]', 'Wait time in milliseconds')
.option('--waitforselector, [waitforselector]', 'Wait for the selector to appear in page')
.option('--el, [el]', 'element css selector')
.option('--auth, [auth]', 'Basic HTTP authentication')
.option('--no, [no]', 'Exclude')
.option('--click, [click]', 'Click')
.option('--file, [file]', 'Output file')
.option('--theme, [theme]', 'Color Theme light or dark')
.option('--vd, [vd]', 'Emulate vision deficiency')
.parse(process.argv);
if (!program.url) {
console.log('Please add --url parameter.\n' +
'Something like this: $ screenshoteer --url http://www.example.com');
process.exit();
}
!program.fullpage ? program.fullPage = true : program.fullPage = JSON.parse(program.fullpage);
console.log(program.url);
console.log(program.fullPage);
const deviceName = puppeteer.devices[program.emulate];
(async () => {
try {
await execute();
} catch(e) {
console.error(e);
process.exit(1);
}
async function execute() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
if (program.no) {
await page.setRequestInterception(true);
page.on('request', request => {
if (request.resourceType() === program.no)
request.abort();
else
request.continue();
});
}
const timestamp = new Date().getTime();
if (program.w || program.h) {
const newWidth = !program.w?600:program.w
const newHeight = !program.h?'0':program.h
if (program.h && !program.fullpage) program.fullPage = false;
await page.setViewport({width: Number(newWidth), height: Number(newHeight)})
}
if (program.theme) {
await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: program.theme }]);
}
if (program.vd) {
await page.emulateVisionDeficiency(program.vd);
}
if (program.emulate)
await page.emulate(deviceName);
else
program.emulate = '';
if (program.auth) {
const [username, password] = program.auth.split(';');
await page.authenticate({ username, password });
}
await page.goto(program.url);
const title = (await page.title()).replace(/[/\\?%*:|"<>]/g, '-');
if (program.waitfor) await page.waitFor(Number(program.waitfor));
if (program.waitforselector) await page.waitForSelector(program.waitforselector);
if (program.click) await page.click(program.click);
const file = program.file ? program.file : `${title} ${program.emulate} ${program.el} ${timestamp}.png`;
if (program.el) {
const el = await page.$(program.el);
await el.screenshot({path: file});
} else {
await page.screenshot({path: file, fullPage: program.fullPage});
}
await page.emulateMedia('screen');
if (program.pdf) await page.pdf({path: `${title} ${program.emulate} ${timestamp}.pdf`});
console.log(title);
await browser.close();
}
})()