-
Notifications
You must be signed in to change notification settings - Fork 1
/
creepy.js
134 lines (108 loc) · 4.23 KB
/
creepy.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
var system = require('system');
(function(host) {
function Creepy(options) {
if(!(this instanceof Creepy)) {
return new Creepy(options);
}
options = options || {};
this.visited = {};
this.rootUrl = options.rootUrl;
this.initialDelay = options.initialDelay || 5000;
this.delay = options.delay || this.initialDelay;
this.urls = [];
this.sort = options.sort || function(u) { return u; };
if(!!options.ignore && options.ignore.length > 0) {
this.ignore = new RegExp(options.ignore.join('|'));
} else {
this.ignore = {
test: function() {
return false;
}
};
}
}
Creepy.webpage = require('webpage');
Creepy.prototype = {
crawl: function(onSuccess, onFailure, onFinished) {
this.onSuccess = onSuccess;
this.onFailure = function() { onFailure.apply(null, arguments); onFinished(); };
this.onFinished = onFinished;
var url = this.rootUrl,
_this = this;
if(url[url.length - 1] === '/') {
url = url.slice(0, -1);
}
var page = Creepy.webpage.create();
page.onConsoleMessage = function (msg) {
system.stderr.writeLine('console: ' + msg);
};
page.open(url, function(status) {
if(status === 'fail') {
page.close();
_this.onFailure({
url: url,
status: status
});
return;
}
_this.urls.push(url);
var length = _this.urls.length;
function visit() {
if(_this.urls.length === 0) {
_this.onFinished();
return;
}
if(length < _this.urls.length) {
_this.urls = _this.sort(_this.urls);
}
length = _this.urls.length;
_this.visit(page, _this.urls.shift(), visit);
}
setTimeout(visit, _this.initialDelay);
});
},
visit: function(page, url, done) {
if (this.visited[url]) {
done();
return;
}
var _this = this;
console.log('Getting content for url:', url);
page.evaluate(function (url) {
window.history.pushState(null, '', url);
window.dispatchEvent(new Event('popstate'));
}, url);
setTimeout(function () {
var html = page.evaluate(function() {
return document.documentElement.innerHTML;
});
_this.visited[url] = true;
_this.urls.push.apply(_this.urls, _this.getUrls(page));
_this.onSuccess({
url: url,
content: html
});
done();
}, _this.delay);
},
getUrls: function(page) {
var _this = this;
return page.evaluate(function() {
return Array.prototype.map.call(document.getElementsByTagName('a'), function(link) {
var href = link.href;
if (window.getComputedStyle(link).display === 'none') {
return;
}
if(href[href.length - 1] === '/') {
href = href.slice(0, -1);
}
return href.split('?')[0].split('#')[0].toLowerCase();
});
}).filter(function(href) {
return href && href.indexOf('/undefined/') === -1 && href.indexOf('http') === 0 && href.indexOf(_this.rootUrl) > -1 && !_this.ignore.test(href) && !_this.visited[href];
});
}
};
host.Creepy = Creepy;
})(phantom);
module.exports = exports = phantom.Creepy;