This repository was archived by the owner on Jan 3, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathwebviewchecker.js
More file actions
192 lines (175 loc) · 6.48 KB
/
Copy pathwebviewchecker.js
File metadata and controls
192 lines (175 loc) · 6.48 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
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
/**
* This casper.js script gets all internal links, images, js, css files.
*
* Usage:
*
* $ casperjs webviewchecker.js http://mysite.tld/
* $ casperjs webviewchecker.js http://mysite.tld/ --max-depth=42
*/
/*global URI*/
var casper = require("casper").create({
pageSettings: {
loadImages: true,
loadPlugins: false
}
});
var fs = require('fs');
var checked = [];
var currentLink = 0;
var fs = require('fs');
var upTo = ~~casper.cli.get('max-depth') || 100;
var url = casper.cli.get(0);
var baseUrl = url;
var links = [url];
var imagelist = [];
var scriptlist = [];
var csslist = [];
var utils = require('utils');
var f = utils.format;
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};
function absPath(url, base) {
return new URI(url).resolve(new URI(base)).toString();
}
// Clean links
function cleanLinks(urls, base) {
return utils.unique(urls).filter(function(url) {
return url.indexOf(baseUrl) === 0 || !new RegExp('^(#|ftp|javascript|http)').test(url);
}).map(function(url) {
return absPath(url, base);
}).filter(function(url) {
return checked.indexOf(url) === -1;
});
}
// Opens the page, perform tests and fetch next links
function crawl(link) {
this.start().then(function() {
this.echo(link, 'COMMENT');
this.open(link);
checked.push(link);
});
this.then(function() {
if (this.currentHTTPStatus === 404) {
this.warn(link + ' is missing (HTTP 404)');
} else if (this.currentHTTPStatus === 500) {
this.warn(link + ' is broken (HTTP 500)');
} else {
this.echo(link + f(' is okay (HTTP %s)', this.currentHTTPStatus));
}
});
this.then(function() {
this.waitForSelector('div.logo', function then() {
console.log('Connexions logo found!');
this.wait(500, function() {
this.capture('Connexions.png');
console.log('Screenshot Connexions!');
this.waitWhileSelector('div.progress.progress-striped.active', function then() {
console.log('Progressbar away');
this.wait(500, function() {
// sorry this is redundant for now with timeout... did know how to do it better for now.
var newLinks = searchLinks.call(this);
links = links.concat(newLinks).filter(function(url) {
return checked.indexOf(url) === -1;
});
this.echo(newLinks.length + " new links found on " + link);
var newImages = searchImages.call(this);
imagelist = imagelist.concat(newImages).unique();
var newScript = searchScripts.call(this);
scriptlist = scriptlist.concat(newScript).unique();
var newCss = searchCss.call(this);
csslist = csslist.concat(newCss).unique();
});
}, function timeout() {
console.log('Progressbar Timeout! :(');
this.capture('timeout.png');
var newLinks = searchLinks.call(this);
links = links.concat(newLinks).filter(function(url) {
return checked.indexOf(url) === -1;
});
this.echo(newLinks.length + " new links found on " + link);
var newImages = searchImages.call(this);
imagelist = imagelist.concat(newImages).unique();
var newScript = searchScripts.call(this);
scriptlist = scriptlist.concat(newScript).unique();
var newCss = searchCss.call(this);
csslist = csslist.concat(newCss).unique();
}, timeout=20000);
});
}, function timeout() { // step to execute if check has failed
// this.echo("I can't haz my screenshot.").exit();
console.log('Connexions logo Timeout! :(');
});
});
}
// Fetch all <a> elements from the page and return
// the ones which contains a href starting with 'http://'
function searchLinks() {
return cleanLinks(this.evaluate(function _fetchInternalLinks() {
return [].map.call(__utils__.findAll('a[href]'), function(node) {
return node.getAttribute('href');
});
}), this.getCurrentUrl());
}
// Get all <img> with a src
function searchImages() {
return cleanLinks(this.evaluate(function _fetchInternalImages() {
return [].map.call(__utils__.findAll('img[src]'), function(node) {
return node.getAttribute('src');
});
}), this.getCurrentUrl());
}
function searchScripts() {
return cleanLinks(this.evaluate(function _fetchInternalImages() {
return [].map.call(__utils__.findAll('script[src]'), function(node) {
return node.getAttribute('src');
});
}), this.getCurrentUrl());
}
function searchCss() {
return cleanLinks(this.evaluate(function _fetchInternalImages() {
return [].map.call(__utils__.findAll('link[href]'), function(node) {
return node.getAttribute('href');
});
}), this.getCurrentUrl());
}
// As long as it has a next link, and is under the maximum limit, will keep running
function check() {
if (links[currentLink] && currentLink < upTo) {
crawl.call(this, links[currentLink]);
currentLink++;
this.run(check);
} else {
this.echo("All done, " + checked.length + " links checked.");
fs.write('links.txt', checked);
fs.write('images.txt', imagelist);
fs.write('css.txt', csslist);
fs.write('scripts.txt', scriptlist);
this.exit();
}
}
if (!url) {
casper.warn('No url passed, aborting.').exit();
}
casper.start('https://js-uri.googlecode.com/svn/trunk/lib/URI.js', function() {
var scriptCode = this.getPageContent() + '; return URI;';
window.URI = new Function(scriptCode)();
if (typeof window.URI === "function") {
this.echo('URI.js loaded');
} else {
this.warn('Could not setup URI.js').exit();
}
});
casper.run(process);
function process() {
casper.start().then(function() {
this.echo("Starting");
}).run(check);
}