-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawl-for-iframes.js
executable file
·445 lines (371 loc) · 12.4 KB
/
crawl-for-iframes.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
"use strict";
const crawlerConfig = Object.freeze({
recursionDepth: 1, // probably reduce maxSubPages if you increase this!
maxSubPages: 99, // 100 total, including homepage of site
scriptTimeout: 20000, // ms
pageTimeout: 20000, // ms
sitesListFilePath: require('process').argv[2],
outputDirPath: "output",
takeScreenshots: false,
});
const browserConfig = Object.freeze({
logLevel: 'error',
path: '/',
capabilities: {
browserName: 'firefox',
'moz:firefoxOptions': {
// Available options:
// https://firefox-source-docs.mozilla.org/testing/geckodriver/Capabilities.html
binary: require('process').env.IFRAME_CRAWLER_FIREFOX_BIN,
args: [
"-headless", // Comment out this line to see the browser!
//"-profile", "/path/to/profile",
],
prefs: {
"dom.element.transform-getters.enabled": true,
"fission.autostart": true,
//"fission.frontend.simulate-events": true,
//"fission.frontend.simulate-messages": true,
//"fission.rebuild_frameloaders_on_remoteness_change": true,
//"fission.preserve_browsing_contexts": true,
//"fission.oopif.attribute": true,
}
},
}
});
const { remote } = require('webdriverio');
const fs = require('fs');
const fsPromises = require('fs').promises;
const readline = require('readline');
const process = require('process');
/**
* This function is copied to, and run in, the window of each webpage we're
* interested in. It gathers all the data that we need in one go before
* returning it via a JSON object. Doing it this way rather than by using
* the normal WebDriver document object API calls means that we avoid a lot of
* IPC traffic, which should be faster.
*/
function gatherPageInfo(getLinks, maxLinkCount) {
function isSameishHostname(homepageHostname, otherHostname) {
if (homepageHostname == otherHostname) {
return true;
}
return otherHostname.endsWith("." + homepageHostname.replace(/^www\./, ""));
}
function toURL(url) {
try {
url = new URL(url);
} catch (e) {
// not actually a valid URL
return null;
}
return url;
}
function toURLWithoutHash(url) {
url = toURL(url);
if (url) {
url.hash = "";
}
return url;
}
function getDataForEmbeddingElement(elem) {
let url = toURL(elem.localName == "object" ? elem.data : elem.src);
if (!url) {
return null;
}
let bounds = elem.getBoundingClientRect();
let data = {
url: url,
bounds: {
x: bounds.x,
y: bounds.y,
w: bounds.width,
h: bounds.height,
},
};
if (getComputedStyle(elem).display == "none") {
data.isDisplayNone = true;
}
if (elem.getTransformToViewport) {
let m = elem.getTransformToViewport();
if (!m.isIdentity) {
data.transform = m.toString().replace(/ /g, "");
}
}
let hasFilter = false, hasClipPath = false, hasMask = false;
let e = elem;
do {
let cs = getComputedStyle(e);
if (cs.filter != "none") {
data.hasFilter = true;
}
if (cs.mask != "none") {
data.hasMask = true;
}
if (cs.clipPath != "none") {
data.hasClipPath = true;
}
} while ((e = e.parentElement));
return data;
}
function getURLForLink(elem) {
// We want unique documents, so we ignore any hash in the links
let url = toURLWithoutHash(elem.href);
if (!url || (url.protocol != "http:" && url.protocol != "https:")) {
return ""; // ignore this link
}
return url.href;
}
function deduplicate(array) {
return [...new Set(array)]
}
const docHostname = document.location.hostname;
const docURL = document.location.href;
if (document.readyState == "loading") {
// If we didn't even reach the "interactive" stage, don't even try to look
// at the content.
return JSON.stringify({
url: docURL,
loadNotComplete: true,
subdocs: [],
links: [],
});
}
const readyState = document.readyState; // stored before we call querySelectorAll
let subdocsData =
deduplicate([...document.querySelectorAll("iframe, embed, object")]
.map(getDataForEmbeddingElement)
.filter(data => data != null));
let links = [];
if (getLinks) {
links =
deduplicate([...document.links]
// only want to crawl site-internal links:
.filter(elem => isSameishHostname(docHostname, elem.hostname))
.map(getURLForLink)
.filter(url => !!url && url != toURLWithoutHash(docURL).href));
// Limit the number of pages we crawl:
while (links.length > maxLinkCount) {
let randomIndex = Math.round(Math.random() * (links.length - 1));
links.splice(randomIndex, 1); // Remove random link
}
}
let result = {
url: document.location.href,
subdocs: subdocsData,
links: links,
};
if (readyState != "complete") {
result.loadNotComplete = true;
}
return JSON.stringify(result);
}
let sitesCSVFile = null; // list of sites to crawl
let outputFile = null; // file to output crawl results to
let screenshotsOutputFile = null; // file to output crawl screenshots to
let browser = null;
let processInterupted = false; // user sent SIGINT (^C)
async function cleanup(code) {
if (browser) {
// Ensure that we close the WebDriver session otherwise we won't be able
// to rerun this script without restarting the geckodriver process first:
await browser.deleteSession();
}
if (screenshotsOutputFile) {
await screenshotsOutputFile.close();
}
if (outputFile) {
await outputFile.close();
}
if (sitesCSVFile) {
await sitesCSVFile.close();
}
}
process.once('SIGINT', async (code) => {
processInterupted = true;
// Try to make the output JSON valid. This may not work.
await fsPromises.writeFile(outputFile, ` ]
}
]
`);
await cleanup(code);
});
async function createOutputFile(suffix = ".json") {
await fsPromises.mkdir(crawlerConfig.outputDirPath, { recursive: true }); // ensure exists
const dateTime =
new Date().toISOString().replace(/T/, '--').replace(/:/g, '-').replace(/\..+/, '');
const fileName = `crawl--${dateTime}${suffix}`;
return fsPromises.open(crawlerConfig.outputDirPath + "/" + fileName, 'w');
}
async function recreateBrowser() {
if (browser) {
try {
await browser.deleteSession();
} catch (e) {
// Ignore "no session" errors
}
}
browser = await remote(browserConfig);
await browser.setTimeouts(crawlerConfig.scriptTimeout,
crawlerConfig.pageTimeout,
undefined);
}
const boundsRE = /bounds\(([^,]+),([^,]+),([^,]+),([^,]+)\)/;
async function maybeTakeScreenshots(result) {
function isSameOrigin(pageURL, subdocURL) {
try {
if (subdocURL == "about:blank" || subdocURL.startsWith("javascript:")) {
return true;
}
pageURL = new URL(pageURL);
subdocURL = new URL(subdocURL);
return pageURL.origin == subdocURL.origin;
} catch(e) {
// Broken URLs aren't interesting; don't add them to the cross-origin list.
return true;
}
}
let firstScreenshot = true;
for (let i = 0; i < result.subdocs.length; ++i) {
let subdoc = result.subdocs[i];
let b = subdoc.bounds;
if (!subdoc.isDisplayNone
&& !isSameOrigin(result.url, subdoc.url)
&& b.w > 5 && b.h > 5
&& (b.x + b.w > 0) && (b.y + b.h > 0)) { // on screen
await browser.execute((b) => scrollTo(b.x, b.y), b);
let output = firstScreenshot ? result.url + "\n" : "";
output += " " + subdoc.url + "\n";
output += " data:image/png;base64," + (await browser.takeScreenshot()) + "\n";
await fsPromises.writeFile(screenshotsOutputFile, output);
firstScreenshot = false;
}
}
/*
if (oopSubdocIndexes.length > 0) {
await browser.execute(() => scrollTo(b.x, b.y));
let embeddingElements = await browser.findElements("css selector", "iframe, embed, object");
for (let i of oopSubdocIndexes) {
// Why in scrollIntoView not a function?!
await (embeddingElements[i].scrollIntoView());
let screenshot = " data:image/png;base64," + (await browser.takeScreenshot()) + "\n";
await fsPromises.writeFile(screenshotsOutputFile, result.url + "\n" + screenshot);
}
}
*/
}
async function processPage(pageURL, outputFile, recursionLevelsLeft, isFirstPage = false) {
if (processInterupted) {
return; // got SIGINT
}
async function writePageJSON(result) {
const indent = " ";
let json = JSON.stringify(result, null, 2).split("\n");
if (!isFirstPage) {
json[0] = json[0].replace("{", ",{")
}
json[0] = indent + json[0];
json = json.join("\n" + indent);
await fsPromises.writeFile(outputFile, json + "\n");
}
async function processException(e, pageURL, outputFile, recursionLevelsLeft) {
if (e.message.indexOf("Timeout loading page after ") != -1) {
// We just ignore timeouts and continue on to examine the page,
// even though it's still loading. (The log output will have
// "incomplete:" added to the start of the output URL.)
return "loadNotComplete";
}
// Skip this page for any other errors.
// Browser crashed
// "Request failed due to insecure certificate"
// "Reached error page"
// redirect loops
// etc.
let crashed =
// Browser crashed:
e.message.indexOf("Tried to run command without establishing a connection") != -1 ||
// Content process crashed:
e.message.indexOf("Browsing context has been discarded") != -1;
if (crashed) {
let logSummary = crashed ? "skipping crashed page" : "skipping page";
let recursionLevel = crawlerConfig.recursionDepth - recursionLevelsLeft;
console.log(`Error: ${logSummary} (level: ${recursionLevel}): ${pageURL} (${e.message})`);
await recreateBrowser();
return "crashed";
}
return "error";
}
let result;
let loadNotComplete = false;
try {
console.log(`Loading page: ${pageURL}`);
await browser.navigateTo(pageURL);
} catch(e) {
let etype = await processException(e, pageURL, outputFile, recursionLevelsLeft);
if (etype != "loadNotComplete") {
result = { url: pageURL, subdocs: [] };
result[etype] = true;
await writePageJSON(result, outputFile);
return;
}
loadNotComplete = true;
}
try {
result = JSON.parse(await browser.execute(gatherPageInfo, /*getLinks*/ recursionLevelsLeft > 0, crawlerConfig.maxSubPages));
if (result === null) {
// XXX Some sort of webdriverio/geckodriver bug where the JSON isn't
// transferred correctly?!? But specifically happening on academia.edu.
throw new Error("null JSON");
}
} catch(e) {
let etype = await processException(e, pageURL, outputFile, recursionLevelsLeft);
result = { url: pageURL, subdocs: [] };
result[etype] = true;
await writePageJSON(result, outputFile);
return;
}
let links = result.links;
delete result.links;
await writePageJSON(result, outputFile);
if (crawlerConfig.takeScreenshots) {
await maybeTakeScreenshots(result);
}
if (recursionLevelsLeft > 0) {
for (let linkURL of links) {
await processPage(linkURL, outputFile, recursionLevelsLeft - 1);
}
}
}
async function main() {
outputFile = await createOutputFile();
if (crawlerConfig.takeScreenshots) {
screenshotsOutputFile = await createOutputFile("-screenshots.txt");
}
await recreateBrowser();
sitesCSVFile = readline.createInterface({
input: fs.createReadStream(crawlerConfig.sitesListFilePath),
terminal: false,
crlfDelay: Infinity // allow any line ending types
});
await fsPromises.writeFile(outputFile, "[\n");
let prefix = "";
for await (let line of sitesCSVFile) {
let hostname = line.substr(line.indexOf(',') + 1);
await fsPromises.writeFile(outputFile, ` ${prefix}{
"hostname": "${hostname}",
"pages": [
`);
await processPage("http://" + hostname, outputFile, crawlerConfig.recursionDepth, /* isFirstPage */ true);
await fsPromises.writeFile(outputFile, ` ]
}
`);
prefix = ",";
// To keep memory use from running away, close the browser and start a new
// instance:
await recreateBrowser();
}
await fsPromises.writeFile(outputFile, "]\n");
}
main().catch(async (e) => {
console.error(e);
}).finally(cleanup);