-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshot.js
119 lines (119 loc) · 3.95 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const commander_1 = require("commander");
const fs_1 = tslib_1.__importDefault(require("fs"));
const jimp_1 = tslib_1.__importDefault(require("jimp"));
const puppeteer_1 = tslib_1.__importDefault(require("puppeteer"));
const package_json_1 = tslib_1.__importDefault(require("./package.json"));
const program = new commander_1.Command();
const help = () => {
program.outputHelp();
console.log('If Width or Height are not specified, a Full Page screenshot will be taken');
process.exit(0);
};
program
.version(package_json_1.default.version)
.addOption(new commander_1.Option('-u, --url <url>', 'URL (website) to screenshot').makeOptionMandatory(true))
.addOption(new commander_1.Option('-t, --time [s]', 'Number of seconds to wait for page to load').default(3))
.addOption(new commander_1.Option('-x, --x [x]', 'Leftmost Pixel').default(0))
.addOption(new commander_1.Option('-y, --y [y]', 'Top Pixel').default(0))
.addOption(new commander_1.Option('-w, --width [width]', 'Width').default(1920))
.addOption(new commander_1.Option('-h, --height [height]', 'Height').default(1080))
.addOption(new commander_1.Option('-o, --out [out]', 'Absolute or Relative Path to save the screenshot'))
.addOption(new commander_1.Option('-c, --crop', 'Auto crop same-color borders'))
.addOption(new commander_1.Option('-d, --debug', 'Prints debug messages'))
.addOption(new commander_1.Option('-a, --auth [auth]', 'NTLM Credentials in username:password format'))
.parse(process.argv);
const options = program.opts();
let url = options.url;
if (!url) {
help();
}
else {
url = url.trim().toLowerCase();
}
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = `http://${url}`;
}
let out = options.out;
if (!out) {
const sections = url.split('/');
const count = url.endsWith('/') ? 2 : 1;
out = `${sections[sections.length - count]}.png`;
}
let credentials;
const auth = options.auth;
if (auth && auth.indexOf(':') !== -1) {
credentials = {
username: auth.split(':')[0],
password: auth.split(':')[1]
};
}
const tmp = out + '_tmp.png';
const time = Number(options.time) * 1000 || 3000;
const clip = {
x: Number(options.x),
y: Number(options.y),
width: Number(options.width),
height: Number(options.height)
};
const vPort = {
width: 1920,
height: 1080,
isLandscape: true
};
const screenshot = {
clip,
path: tmp
};
const launch = { headless: true, args: ['--no-sandbox'] };
const debugFlag = options.debug;
if (debugFlag) {
launch.headless = false;
}
const debug = (message) => {
if (debugFlag) {
console.debug(message);
}
};
(async () => {
try {
const browser = await puppeteer_1.default.launch(launch);
debug('Browser Opened');
const page = await browser.newPage();
debug('Page Created');
await page.setViewport(vPort);
debug('Viewport Set');
if (credentials) {
await page.authenticate(credentials);
debug('Credentials Entered');
}
await page.goto(url);
await sleep(time);
debug('Page Loaded');
await page.screenshot(screenshot);
debug('Screenshot Taken');
await page.close();
debug('Page Closed');
await browser.close();
debug('Browser Closed');
if (options.crop) {
const jimp = await jimp_1.default.read(tmp);
await jimp.autocrop(false).write(out);
await fs_1.default.unlinkSync(tmp);
debug('Image Cropped');
}
else {
await fs_1.default.renameSync(tmp, out);
debug('Image Saved');
}
}
catch (e) {
console.log('Screenshot Failed');
console.error(e);
process.exit(1);
}
})();
const sleep = (delay) => new Promise(resolve => setTimeout(resolve, delay));