forked from schnerd/chrome-headless-screenshots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (88 loc) · 3.14 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
100
101
102
103
const CDP = require('chrome-remote-interface');
const argv = require('minimist')(process.argv.slice(2));
const file = require('mz/fs');
const timeout = require('delay');
// CLI Args
const url = argv.url || 'https://www.google.com';
const format = argv.format === 'jpeg' ? 'jpeg' : 'png';
const viewportWidth = argv.viewportWidth || 1440;
let viewportHeight = argv.viewportHeight || 900;
const delay = argv.delay || 0;
const userAgent = argv.userAgent;
const fullPage = argv.full;
const output = argv.output || `output.${format === 'png' ? 'png' : 'jpg'}`;
init();
async function init() {
let client;
try {
// Start the Chrome Debugging Protocol
client = await CDP();
// Verify version
const { Browser } = await CDP.Version();
const browserVersion = Browser.match(/\/(\d+)/)[1];
if (Number(browserVersion) !== 60) {
console.warn(`This script requires Chrome 60, however you are using version ${browserVersion}. The script is not guaranteed to work and you may need to modify it.`);
}
// Extract used DevTools domains.
const {DOM, Emulation, Network, Page, Runtime} = client;
// Enable events on domains we are interested in.
await Page.enable();
await DOM.enable();
await Network.enable();
// If user agent override was specified, pass to Network domain
if (userAgent) {
await Network.setUserAgentOverride({userAgent});
}
// Set up viewport resolution, etc.
const deviceMetrics = {
width: viewportWidth,
height: viewportHeight,
deviceScaleFactor: 0,
mobile: false,
fitWindow: false,
};
await Emulation.setDeviceMetricsOverride(deviceMetrics);
await Emulation.setVisibleSize({
width: viewportWidth,
height: viewportHeight,
});
// Navigate to target page
await Page.navigate({url});
// Wait for page load event to take screenshot
await Page.loadEventFired();
await timeout(delay);
// If the `full` CLI option was passed, we need to measure the height of
// the rendered page and use Emulation.setVisibleSize
if (fullPage) {
const {root: {nodeId: documentNodeId}} = await DOM.getDocument();
const {nodeId: bodyNodeId} = await DOM.querySelector({
selector: 'body',
nodeId: documentNodeId,
});
const {model} = await DOM.getBoxModel({nodeId: bodyNodeId});
viewportHeight = model.height;
await Emulation.setVisibleSize({width: viewportWidth, height: viewportHeight});
// This forceViewport call ensures that content outside the viewport is
// rendered, otherwise it shows up as grey. Possibly a bug?
await Emulation.forceViewport({x: 0, y: 0, scale: 1});
}
const screenshot = await Page.captureScreenshot({
format,
fromSurface: true,
clip: {
width: viewportWidth,
height: viewportHeight
}
});
const buffer = new Buffer(screenshot.data, 'base64');
await file.writeFile(output, buffer, 'base64');
console.log('Screenshot saved');
client.close();
} catch (err) {
if (client) {
client.close();
}
console.error('Exception while taking screenshot:', err);
process.exit(1);
}
}