-
Notifications
You must be signed in to change notification settings - Fork 6
/
phantomScript.js
151 lines (122 loc) · 3.7 KB
/
phantomScript.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
/*
* Phantom script to create a cookies.txt file with the serialized cookies
* Usage : phantomjs phantomCookie.js {...}
*/
var page = require('webpage').create(),
system = require('system'),
options = JSON.parse(system.args[1]),
metrics = {req: 0, size:0, time:0, errors: [], jsErrors: []};
if(options.cookies) {
for (var i = 0; i < options.cookies.length; i++) {
phantom.addCookie(options.cookies[i]);
}
}
if(options.ua) {
page.settings.userAgent = options.ua;
}
page.viewportSize = {
width: options.viewportSize.width,
height: options.viewportSize.height
};
// requests to exclude (trackers ...)
var blacklist = options.blacklist || [];
var renderTimeout = 0;
page.resources = [];
if (options.url) {
// ultimate check in case something is broken
var security = securityTimer();
page.onResourceRequested = function (req) {
if(isblacklisted(req)){
clearTimeout(renderTimeout);
if(security.cleared){
security = securityTimer();
}
page.resources[req.id] = {
request: req,
startReply: null,
endReply: null
};
}
};
page.onResourceReceived = function (res) {
if (!res.stage || res.stage === 'start' && isblacklisted(res)) {
page.resources[res.id].startReply = res;
}
if ((!res.stage || res.stage === 'end') && isblacklisted(res)) {
//mark saved request as finished
page.resources[res.id].endReply = res;
//render if no other request is made after 1000ms
if (page.resources.filter(isNotFinished).length === 0) {
security.clear();
renderTimeout = setTimeout(doRender, 1000);
}
}
};
page.onError = function(msg) {
metrics.jsErrors.push(msg);
};
page.open('http://' + options.url, function(status) {
if (status !== 'success') {
console.log("Error opening url \"" + options.url);
phantom.exit(1);
}
});
} else if(options.body) {
page.content = options.body;
doRender();
}
function isblacklisted (req) {
var isOk = true;
for (var i = 0; i < blacklist.length; i++) {
if(req.url.indexOf(blacklist[i]) !== -1) {
isOk = false;
}
}
return isOk;
}
function securityTimer() {
return new Timeout(function (){
var notEnded = page.resources.filter(isNotFinished);
for (var i = 0; i < notEnded.length; i++) {
metrics.errors.push(notEnded[i].request.url);
}
doRender();
}, 30000);
}
function Timeout(fn, interval) {
var id = setTimeout(fn, interval);
this.cleared = false;
this.clear = function () {
this.cleared = true;
clearTimeout(id);
};
}
function isNotFinished (el) {
return (el.endReply === null);
}
function doRender() {
metrics.req = page.resources.length;
page.resources.forEach(function (resource) {
var request = resource.request,
startReply = resource.startReply,
endReply = resource.endReply;
if (!request || !startReply || !endReply) {
return;
}
// console.log(startReply);
if (request.url.match(/(^data:image\/.*)/i)) {
return;
}
metrics.size += startReply.bodySize;
metrics.time += endReply.time - request.time;
});
// Will be intercept by zeno
console.log(JSON.stringify(metrics));
setTimeout(function() {
page.render(options.path);
page.close();
phantom.exit();
}, 200);
}
function contains(a, b){return !!~a.indexOf(b); }