-
Notifications
You must be signed in to change notification settings - Fork 0
/
broken-links.js
67 lines (60 loc) · 1.69 KB
/
broken-links.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
require('dotenv').config()
const puppeteer = require('puppeteer');
var fs = require('fs');
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
async function getImages(url , index=0, browser){
let username = process.env.HT_USER;
let password = process.env.HT_PASS;
let page = await browser.newPage();
// let old_url = process.env.OLD_DOMAIN.replace(/\/$/, '') + '/' +url;
let new_url = process.env.NEW_DOMAIN.replace(/\/$/, '') + '/' +url;
try {
if (username && password) {
await page.authenticate({username, password});
}
await page.goto(new_url, {
waitUntil: 'networkidle2',
timeout: 0
});
console.log(`checking ${new_url}`);
let images_links = await page.$$eval('body.error404', image => image);
if (images_links.length > 0) {
// await page.screenshot({path: `page-${index}.png`, fullPage: true });
console.log(new_url);
fs.appendFile('broken-links.txt', new_url+'\n', function (err) {
if (err) throw err;
console.log('Saved!');
});
}
// await browser.close();
}
catch (e){
console.error(e);
}
}
(async() => {
fs.writeFile('broken-links.txt', '', function (err) {
if (err) throw err;
console.log('Replaced!');
});
const same_site_links = process.env.SAME_SITE_LINKS.split(',');
try {
await asyncForEach(same_site_links, async (link, index) => {
try {
let browser = await puppeteer.launch();
await getImages(link, index, browser);
await browser.close();
}
catch (e){
console.error(e);
}
});
}
catch (e){
console.error(e);
}
})();