-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.js
49 lines (45 loc) · 1.46 KB
/
check.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
"use strict";
var page = require('webpage').create(),
system = require('system');
page.onError = function(msg, trace) {
return true;
};
if (system.args.length === 1) {
console.log('Usage: check.js <URL>');
phantom.exit(1);
} else {
page.address = system.args[1];
page.resources = [];
page.onResourceRequested = function (req) {
page.resources.push(req.url);
};
page.open(page.address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
phantom.exit(1);
} else {
var links = page.evaluate(function () {
var linkQuery = document.querySelectorAll('a');
return Array.prototype.map.call(linkQuery, function (elem) {
return elem.href;
});
});
var allLinks = page.resources.concat(links);
var linksToValidate = [];
var host = '://' + page.evaluate(function() {
return window.location.hostname;
});
allLinks.forEach(function (link) {
if (link.lastIndexOf("http", 0) === 0) {
if (link.indexOf(host) === -1) {
linksToValidate.push(link);
}
}
});
linksToValidate.forEach(function (link) {
console.log(link);
});
phantom.exit();
}
});
}