forked from sindresorhus/capture-website
/
index.js
240 lines (186 loc) · 5.84 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict';
const {promisify} = require('util');
const fs = require('fs');
const fileUrl = require('file-url');
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const toughCookie = require('tough-cookie');
const writeFile = promisify(fs.writeFile);
const isUrl = string => /^(https?|file):\/\/|^data:/.test(string);
const hideElements = elements => {
for (const element of elements) {
element.style.visibility = 'hidden';
}
};
const removeElements = elements => {
for (const element of elements) {
element.style.display = 'none';
}
};
const getBoundingClientRect = element => {
const {height, width, x, y} = element.getBoundingClientRect();
return {height, width, x, y};
};
const parseCookie = (url, cookie) => {
if (typeof cookie === 'object') {
return cookie;
}
const jar = new toughCookie.CookieJar(undefined, {rejectPublicSuffixes: false});
jar.setCookieSync(cookie, url);
const ret = jar.serializeSync().cookies[0];
// Use this instead of the above when the following issue is fixed:
// https://github.com/salesforce/tough-cookie/issues/149
// const ret = toughCookie.parse(cookie).serializeSync();
ret.name = ret.key;
delete ret.key;
return ret;
};
const captureWebsite = async (url, options) => {
const finalUrl = isUrl(url) ? url : fileUrl(url);
options = {
width: 1280,
height: 800,
scaleFactor: 2,
fullPage: false,
defaultBackground: true,
timeout: 60, // The Puppeteer default of 30 is too short
delay: 0,
debug: false,
launchOptions: {},
_keepAlive: false,
...options
};
const timeoutInSeconds = options.timeout * 1000;
const viewportOptions = {
width: options.width,
height: options.height,
deviceScaleFactor: options.scaleFactor
};
const screenshotOptions = {};
if (options.type) {
screenshotOptions.type = options.type;
}
if (options.quality) {
screenshotOptions.quality = options.quality * 100;
}
if (options.fullPage) {
screenshotOptions.fullPage = options.fullPage;
}
if (options.defaultBackground) {
screenshotOptions.omitBackground = !options.defaultBackground;
}
const launchOptions = {...options.launchOptions};
if (options.debug) {
launchOptions.headless = false;
launchOptions.slowMo = 100;
}
const browser = options._browser || await puppeteer.launch(launchOptions);
const page = await browser.newPage();
if (options.debug) {
page.on('console', message => {
let {url, lineNumber, columnNumber} = message.location();
lineNumber = lineNumber ? `:${lineNumber}` : '';
columnNumber = columnNumber ? `:${columnNumber}` : '';
const location = url ? ` (${url}${lineNumber}${columnNumber})` : '';
console.log(`\nPage log:${location}\n${message.text()}\n`);
});
page.on('pageerror', error => {
console.log('\nPage error:', error, '\n');
});
// TODO: Add more events from https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#event-requestfailed
}
if (options.authentication) {
await page.authenticate(options.authentication);
}
if (options.cookies) {
const cookies = options.cookies.map(cookie => parseCookie(finalUrl, cookie));
await page.setCookie(...cookies);
}
if (options.headers) {
await page.setExtraHTTPHeaders(options.headers);
}
if (options.userAgent) {
await page.setUserAgent(options.userAgent);
}
await page.setViewport(viewportOptions);
if (options.emulateDevice) {
if (!(options.emulateDevice in devices)) {
throw new Error(`The device name \`${options.emulateDevice}\` is not supported`);
}
await page.emulate(devices[options.emulateDevice]);
}
await page.goto(finalUrl, {
timeout: timeoutInSeconds,
waitUntil: 'networkidle2'
});
if (options.hideElements) {
await Promise.all(options.hideElements.map(selector => page.$$eval(selector, hideElements)));
}
if (options.removeElements) {
await Promise.all(options.removeElements.map(selector => page.$$eval(selector, removeElements)));
}
if (options.clickElement) {
await page.click(options.clickElement);
}
const getInjectKey = (ext, value) => isUrl(value) ? 'url' : value.endsWith(`.${ext}`) ? 'path' : 'content';
if (options.modules) {
await Promise.all(options.modules.map(module_ => {
return page.addScriptTag({
[getInjectKey('js', module_)]: module_,
type: 'module'
});
}));
}
if (options.scripts) {
await Promise.all(options.scripts.map(script => {
return page.addScriptTag({
[getInjectKey('js', script)]: script
});
}));
}
if (options.styles) {
await Promise.all(options.styles.map(style => {
return page.addStyleTag({
[getInjectKey('css', style)]: style
});
}));
}
if (options.waitForElement) {
await page.waitForSelector(options.waitForElement, {
visible: true,
timeout: timeoutInSeconds
});
}
if (options.element) {
await page.waitForSelector(options.element, {
visible: true,
timeout: timeoutInSeconds
});
screenshotOptions.clip = await page.$eval(options.element, getBoundingClientRect);
screenshotOptions.fullPage = false;
}
if (options.delay) {
await page.waitFor(options.delay * 1000);
}
if (options.beforeScreenshot) {
await options.beforeScreenshot(page, browser);
}
const buffer = await page.screenshot(screenshotOptions);
await page.close();
if (!options._keepAlive) {
await browser.close();
}
return buffer;
};
module.exports.file = async (url, filePath, options = {}) => {
const screenshot = await captureWebsite(url, options);
await writeFile(filePath, screenshot, {
flag: options.overwrite ? 'w' : 'wx'
});
};
module.exports.buffer = async (url, options) => captureWebsite(url, {...options, encoding: 'binary'});
module.exports.base64 = async (url, options) => captureWebsite(url, {...options, encoding: 'base64'});
module.exports.devices = Object.values(devices).map(device => device.name);
if (process.env.NODE_ENV === 'test') {
module.exports._startBrowser = puppeteer.launch.bind(puppeteer);
}