-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
executable file
·307 lines (262 loc) · 7.27 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env node
const puppeteer = require('puppeteer');
const yargs = require('yargs');
const fs = require('fs-extra');
const slugify = require('slugify');
yargs.options({
version: {
boolean: true,
describe: 'Show version number',
default: false,
},
help: {
boolean: true,
describe: 'Show help',
default: false,
},
q: {
alias: 'quit',
boolean: true,
describe: 'Be quit',
default: false,
},
t: {
alias: 'tabs',
number: true,
describe: 'Set number of pages',
default: 2,
},
s: {
alias: 'screenshot',
boolean: true,
describe: 'Take a screenshot',
default: false,
},
p: {
alias: 'pdf',
boolean: true,
describe: 'Take a PDF',
default: false,
},
m: {
alias: 'mhtml',
boolean: true,
describe: 'Save as mhtml',
default: false,
},
r: {
alias: 'recursive',
boolean: true,
describe: 'Recursively visit links',
default: false,
},
l: {
alias: 'level',
number: true,
describe: 'Set recursion depth',
default: 1,
},
w: {
alias: 'width',
number: true,
describe: 'Set page width',
default: 1920,
},
h: {
alias: 'height',
number: true,
describe: 'Set page height',
default: 1080,
},
f: {
alias: 'full-page',
boolean: true,
describe: 'Take a screenshot of the full scrollable page',
default: false,
},
L: {
alias: 'relative',
boolean: true,
describe: 'Follow relative links only',
default: false,
},
['device-scale-factor']: {
number: true,
describe: 'Specify device scale factor',
default: 1,
},
['is-mobile']: {
boolean: true,
describe: 'Take meta viewport into account',
default: false,
},
['has-touch']: {
boolean: true,
describe: 'Support touch events',
default: false,
},
['is-landscape']: {
boolean: true,
describe: 'Set viewport in landscape mode',
default: false,
},
['https-only']: {
boolean: true,
describe: 'Follow HTTPS links only',
default: false,
},
['same-origin']: {
boolean: true,
describe: 'Only visit pages with same origin',
default: false,
},
['disable-js']: {
boolean: true,
describe: 'Disable javascript',
default: false,
},
['user-agent']: {
string: true,
describe: 'Set user agent',
},
['pattern']: {
string: true,
describe: 'Only follow links that match the supplied regular expression',
}
});
yargs.usage('Usage: $0 [OPTION]... [URL]...');
yargs.demandCommand().showHelpOnFail(true).wrap(yargs.terminalWidth());
const argv = yargs.argv;
const { screenshot, pdf, fullPage, width, height, recursive, userAgent, disableJs, mhtml } = argv;
const options = {
args: [
'--incognito',
'--no-experiments',
'--no-pings',
'--no-referrers',
'--dns-prefetch-disable',
'--disable-preconnect',
],
defaultViewport: { ...argv },
};
const TABS = argv.tabs;
const log = argv.quit ? function() {} : console.log;
function findAllLinks({ sameOrigin, httpsOnly, relative, pattern }) {
const allElements = [];
function isAnchor(el) {
return el.localName === 'a' && el.href !== location.href && !el.href.startsWith('mailto') && el.href;
}
function isSameOrigin(el) {
return sameOrigin ? new URL(el.href).origin === new URL(location.href).origin : true;
}
function isHttps(el) {
return httpsOnly ? el.href.startsWith('https:') : true;
}
function isRelative(el) {
return relative ? el.attributes.href.value.indexOf('://') < 1 && el.attributes.href.value.indexOf('//') !== 0 : true;
}
function isPattern(el) {
return pattern ? new RegExp(pattern).test(el.href) : true;
}
function findAllLinks() {
let links = allElements.filter(isAnchor);
if (sameOrigin) {
links = links.filter(isSameOrigin);
}
if (httpsOnly) {
links = links.filter(isHttps);
}
if (relative) {
links = links.filter(isRelative);
}
if (pattern) {
links = links.filter(isPattern);
}
return links.map(el => el.href);
}
function findAllElements(elements) {
allElements.push(...elements);
for (const e of elements) {
if (e.shadowRoot) {
findAllElements(e.shadowRoot.querySelectorAll('*'));
}
}
}
findAllElements(document.querySelectorAll('*'));
return findAllLinks(allElements);
}
async function urlToPath(url) {
let [, ...paths] = new URL(url).href.split('/');
paths = paths.map(path => slugify(path)).filter(path => path);
if (paths.length < 2) {
paths.push(Math.random().toString(36).slice(2));
}
const dir = paths.slice(0, -1).join('/');
await fs.ensureDir(dir);
return paths.join('/');
}
async function openBrowser(url) {
const browser = await puppeteer.launch(options);
const tabs = [];
const urls = [ url ];
const crawledPages = [];
let depth = 0;
let depthMarker = 1;
async function openTab() {
const page = await browser.newPage();
if (userAgent) {
await page.setUserAgent(argv.userAgent);
}
if (disableJs) {
await page.setJavaScriptEnabled(false);
}
while (urls.length && depth <= argv.level) {
const url = urls.shift();
if (crawledPages.includes(url)) {
continue;
}
log(url);
await page.goto(url, { waitUntil: "networkidle2" });
const path = await urlToPath(url);
if (screenshot) {
await page.screenshot({
path: `${path}.png`,
type: 'png',
fullPage,
});
}
if (pdf) {
await page.pdf({ path: `${path}.pdf`, width, height });
}
if (mhtml) {
const client = await page.target().createCDPSession();
const { data } = await client.send('Page.captureSnapshot', { format: 'mhtml' });
fs.writeFileSync(`${path}.mhtml`, data);
}
if (recursive) {
if (!depthMarker--) {
depth++;
depthMarker = urls.length;
}
if (depth < argv.level) {
const links = await page.evaluate(findAllLinks, argv);
urls.push(...links);
}
}
crawledPages.push(url);
}
await page.close();
}
for (let i = 0; i < TABS; ++i) {
tabs.push(openTab());
}
await Promise.all(tabs);
await browser.close();
}
(async () => {
const browsers = [];
for (const url of argv._) {
browsers.push(openBrowser(url));
}
await Promise.all(browsers);
})();