-
Notifications
You must be signed in to change notification settings - Fork 677
Expand file tree
/
Copy pathcheckredir.js
More file actions
56 lines (50 loc) · 2.22 KB
/
Copy pathcheckredir.js
File metadata and controls
56 lines (50 loc) · 2.22 KB
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
const puppeteer = require('puppeteer')
const readline = require('readline');
const MAX_WORKERS = 20
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
var urls = []
var reading = true
rl.on('line', async (url) => {
urls.push(url) // start queuing the read urls right away
});
(async ()=> { // kick off an async "thread" to read from the queue
const browser = await puppeteer.launch({ignoreHTTPSErrors: true}) // build the browser once
let working = new Set() // maybe not the most memory efficient to make two datastructures
for (let i = 0; i < MAX_WORKERS; i++) { // but the list as a queue is helpful and the set is helpful for different reasons
(async ()=>{ // only make MAX_WORKERS tasks ("threads") so we do not crash chrome
const page = await browser.newPage()
while (urls.length) {
let url = urls.shift() // grab the first URL
working.add(url) // mark that we are working on that URL
try {
await page.goto(url)
var destination = await page.evaluate(() => {
return {"domain": document.domain, "href": document.location.href}
})
var u = new URL(url)
if (u.host != destination.domain){
console.log(`${url} redirects to ${destination.href}`)
} else {
console.log(`${url} does not redirect`)
}
} catch {
// should an error just pass?
console.log(`error checking ${url}`)
} finally {
working.delete(url) // we are no longer working on that URL
}
}
await page.close() // clean up the page object, potentially an issue if page crashes in loop
if (!reading && !working.size) {// I think this will prevent premature browser closure and issues with list/set desync
browser.close()
}
})()
}
})()
rl.on('close', async () => {
reading = false // make sure that our queue and set do not get desynced
})