-
Notifications
You must be signed in to change notification settings - Fork 1
/
chrome-devtools-protocol-screenshot.js
executable file
·126 lines (101 loc) · 3.34 KB
/
chrome-devtools-protocol-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
120
121
122
123
124
125
126
#!/usr/bin/env node
const CDP = require('chrome-remote-interface')
const fs = require('fs')
const path = require('path')
const program = require('commander')
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const intList = val => val.split(',').map(n => parseInt(n))
program
.version('0.1.0')
.description(`Reads instructions from stdin or args and captures screenshots
using the chrome devtools protocol from a running blink-based
browser instance at various given breakpoints.`)
.usage('--breakPoints 1200,800,400 -p 9222')
.option('-o, --outputDir <path>', 'set output directory')
.option(
'-p, --remoteDebuggingPort <port>',
'set remote debugging port',
parseInt
)
.option(
'-b, --breakPoints <b1,b2,b3>',
'breakPoints to take a screenshot at. Only usable with no stdin pipe.',
intList
)
.parse(process.argv);
const dir = path.resolve(program.outputDir || './screenshot_captures');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
if (process.stdin.isTTY) {
singlePage();
} else {
// data was piped
var stdin = process.openStdin()
var data = ""
stdin.on('data', chunk => { data += chunk })
stdin.on('end', () => { withStdIn(JSON.parse(data)) })
}
function withStdIn(data) {
CDP({port: program.remoteDebuggingPort}, async (client) => {
const {Page, Emulation, Runtime} = client;
const originalDimentions = await Page
.getLayoutMetrics()
.then(r => r.layoutViewport)
for ([uri, opt] of Object.entries(data.uris)) {
await Runtime.evaluate({
expression: `window.location="${uri}"`,
returnByValue: true
})
// Give the page time to render before taking a screenshot
await sleep(program.navigationSleep || 0)
for (b of opt.breakPoints) {
await Emulation.setVisibleSize({
width: b,
height: originalDimentions.clientHeight,
});
const b64Img = await Page.captureScreenshot();
const buffer = new Buffer(b64Img.data, 'base64');
fs.writeFileSync(`${dir}/${uri}_${b}.png`, buffer);
console.log(`wrote: ${dir}/${uri}_${b}.png`);
}
}
// Reset the client size.
await Emulation.setVisibleSize({
width: originalDimentions.clientWidth,
height: originalDimentions.clientHeight,
});
client.close();
});
}
function singlePage(data) {
CDP({port: program.remoteDebuggingPort}, async (client) => {
const {Page, Emulation, Runtime} = client;
const originalDimentions = await Page
.getLayoutMetrics()
.then(r => r.layoutViewport)
if (program.breakPoints) {
for(b of program.breakPoints) {
await Emulation.setVisibleSize({
width: b,
height: originalDimentions.clientHeight,
});
const b64Img = await Page.captureScreenshot();
const buffer = new Buffer(b64Img.data, 'base64');
fs.writeFileSync(`${dir}/${b}.png`, buffer);
console.log(`wrote: ${dir}/${b}.png`);
}
} else {
const b64Img = await Page.captureScreenshot();
const buffer = new Buffer(b64Img.data, 'base64');
fs.writeFileSync(`${dir}/out.png`, buffer);
console.log(`wrote: ${dir}/out.png`);
}
// Reset the client size.
await Emulation.setVisibleSize({
width: originalDimentions.clientWidth,
height: originalDimentions.clientHeight,
});
client.close();
});
}