-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathindex.js
463 lines (420 loc) · 12.3 KB
/
index.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
'use strict';
var clone = require('clone');
var defaults = require('defaults');
var phantom = require('node-phantom-simple');
var path = require('path');
var debug = require('debug')('horseman');
var debugv = require('debug')('horseman:verbose');
var actions = require('./actions');
var HorsemanPromise = require('./HorsemanPromise.js');
var noop = function() {};
/**
* Check for npm PhantomJS
*/
var phantomjs;
try {
phantomjs = require('phantomjs-prebuilt');
debug('using PhantomJS from phantomjs-prebuilt module');
} catch (err) {
try {
phantomjs = require('phantomjs');
debug('using PhantomJS from phantomjs module');
} catch (err) {
phantomjs = {};
debug('using PhantomJS from $PATH');
}
}
/**
* Default options.
*/
var DEFAULTS = {
timeout: 5000,
interval: 50,
weak: true,
loadImages: true,
sslProtocol: 'any', //sslv3, sslv2, tlsv1, any
injectJquery: true,
switchToNewTab: false,
injectBluebird: false,
phantomPath: phantomjs.path
};
/**
* Give each instance a unique ID for debug purposes
*/
var instanceId = 0;
function prepare(horseman, instantiationOptions) {
return new HorsemanPromise(function(resolve, reject) {
phantom.create(instantiationOptions, function(err, instance) {
debug('phantom created');
horseman.phantom = instance;
if (err) {
return reject(err);
}
if (debugv.enabled) {
horseman.phantom.onError = function(msg, trace) {
var str = msg;
trace.forEach(function(t) {
var fun = t.function || '<anonymous>';
str += '\n\t' + t.file + ': ' + t.line + ' in ' + fun;
});
return debugv('onPhantomError', str, horseman.id);
};
}
instance.get('version', function(err, ver) {
if (err) { return reject(err); }
var version = ver.major + '.' + ver.minor + '.' + ver.patch;
debug('phantom version ' + version);
return horseman.pageMaker()
.then(resolve)
.catch(reject);
});
});
})
.then(function() {
horseman.pageCnt = 0;
})
.bind(horseman);
}
/**
* Creates a new Horseman.
* @constructor
*/
var Horseman = function Horseman(options) {
this.ready = false;
if (!(this instanceof Horseman)) { return new Horseman(options); }
this.options = defaults(clone(options) || {}, DEFAULTS);
this.id = ++instanceId;
debug('.setup() creating phantom instance %s', this.id);
var phantomOptions = {
'load-images': this.options.loadImages,
'ssl-protocol': this.options.sslProtocol
};
if (typeof this.options.ignoreSSLErrors !== 'undefined') {
phantomOptions['ignore-ssl-errors'] = this.options.ignoreSSLErrors;
}
if (typeof this.options.webSecurity !== 'undefined') {
phantomOptions['web-security'] = this.options.webSecurity;
}
if (typeof this.options.proxy !== 'undefined') {
phantomOptions.proxy = this.options.proxy;
}
if (typeof this.options.proxyType !== 'undefined') {
phantomOptions['proxy-type'] = this.options.proxyType;
}
if (typeof this.options.proxyAuth !== 'undefined') {
phantomOptions['proxy-auth'] = this.options.proxyAuth;
}
if (typeof this.options.diskCache !== 'undefined') {
phantomOptions['disk-cache'] = this.options.diskCache;
}
if (typeof this.options.diskCachePath !== 'undefined') {
phantomOptions['disk-cache-path'] = this.options.diskCachePath;
}
if (typeof this.options.cookiesFile !== 'undefined') {
phantomOptions['cookies-file'] = this.options.cookiesFile;
}
if (this.options.debugPort) {
phantomOptions['remote-debugger-port'] = this.options.debugPort;
phantomOptions['remote-debugger-autorun'] = 'no';
if (this.options.debugAutorun !== false) {
phantomOptions['remote-debugger-autorun'] = 'yes';
}
}
Object.keys(this.options.phantomOptions || {}).forEach(function (key) {
if (typeof phantomOptions[key] !== 'undefined') {
debug('Horseman option ' + key + ' overridden by phantomOptions');
}
phantomOptions[key] = this.options.phantomOptions[key];
}.bind(this));
var instantiationOptions = {
parameters: phantomOptions
};
if (typeof this.options.phantomPath !== 'undefined') {
instantiationOptions['path'] = this.options.phantomPath;
}
// Store the url that was requested for the current url
this.targetUrl = null;
// Store the HTTP status code for resources requested.
this.responses = {};
this.tabs = [];
this.onTabCreated = noop;
this.onTabClosed = noop;
this.ready = prepare(this, instantiationOptions);
};
/**
* Attaches a page to the Horseman instance.
* @param {string} [url=about:blank] - URL to open.
* @param {Page} [_page] - Page to attach. If null, a new Page will be created.
* @emits Horseman#tabCreated
*/
Horseman.prototype.pageMaker = function(url, _page) {
var self = this;
url = url || 'about:blank';
// .try runs synchronously
var loaded;
return HorsemanPromise.try(function getPage() {
var page;
if (_page) {
var p = setupPage(_page);
loaded = _page.loadedPromise();
return p;
} else {
return HorsemanPromise.fromCallback(function(done) {
return self.phantom.createPage(function(err, page) {
if (page) {
var p = setupPage(page).asCallback(done);
loaded = page.loadedPromise();
return p;
}
return done(err, page);
});
})
.tap(function openPage(page) {
return HorsemanPromise.fromCallback(function(done) {
return page.open(url, done);
});
});
}
})
.tap(function waitForLoadFinished() {
return loaded;
})
.tap(function attachPage(page) {
if (!_page || self.options.switchToNewTab) {
self.page = page;
}
var tabNum = self.tabs.push(page) - 1;
return self.onTabCreated(tabNum, page);
})
.bind(self);
/**
* Needs to be called *BEFORE* page creation callback returns
* This is necessary to avoid having events before callbacks are attached
* @param page - Newly created page to decorate and attach to horseman
*/
function setupPage(page) {
debug('page created');
page.onPageCreated = function(newPage) {
return self.ready = self.pageMaker(undefined, newPage)
.tap(function() {
page.onPageCreated2(newPage);
});
};
page.onPageCreated2 = noop;
page.onResourceReceived = function(response) {
self.responses[response.url] = response.status;
};
page.onLoadFinished = loadFinishedSetup;
page.onClosing = function() {
var tabNum = self.tabs.indexOf(page);
debug('closing tab', tabNum);
self.tabs.splice(tabNum, 1);
if (self.page === page) {
// Switch to previous tab when current tab closes
self.page = self.tabs[tabNum - 1];
}
self.onTabClosed(tabNum, page);
};
page.onTimeout = noop;
// Create a Promise for when onLoadFinished callback is done
page.loadedPromise = function() {
return HorsemanPromise.fromCallback(function(done) {
page.loadDone = function(err, res) {
page.loadDone = undefined;
return done(err, res);
};
});
};
// If verbose debug, add default error and consoleMessage handlers
if (debugv.enabled) {
page.onError = function(msg, trace) {
var str = msg;
trace.forEach(function(t) {
var fun = t.function || '<anonymous>';
str += '\n\t' + t.file + ': ' + t.line + ' in ' + fun;
});
return debugv('onError', str, self.id);
};
page.onConsoleMessage = function(msg, lineNum, sourceId) {
var str = msg + ' line: ' + lineNum + ' in ' + sourceId;
return debugv('onConsoleMessage', str, self.id);
};
}
return HorsemanPromise.fromCallback(function(done) {
return page.set(
'settings.resourceTimeout',
self.options.timeout,
done
);
}).return(page);
/**
* Do any javascript injection on the page
* TODO: Consolidate into one page.evaluate?
*/
function loadFinishedSetup(status) {
var args = arguments;
self.pageCnt++;
debug('phantomjs onLoadFinished triggered', status, self.pageCnt);
return self.ready = HorsemanPromise.try(function checkStatus() {
if (status !== 'success') {
var err = new Error('Failed to load url');
return HorsemanPromise.reject(err);
}
})
.then(function injectJQuery() {
if (!self.options.injectJquery) {
return;
}
return HorsemanPromise.fromCallback(function hasJQuery(done) {
return page.evaluate(function hasJQuery() {
return (typeof window.jQuery !== 'undefined');
}, done);
})
.then(function(hasJquery) {
if (hasJquery) {
debug('jQuery not injected - already exists on page');
return;
}
var jQueryLocation = path.join(__dirname,
'../files/jquery-2.1.1.min.js');
return HorsemanPromise.fromCallback(function(done) {
return page.injectJs(jQueryLocation, done);
})
.tap(function(successful) {
if (!successful) {
var err = new Error('jQuery injection failed');
return HorsemanPromise.reject(err);
}
debug('injected jQuery');
});
});
})
.then(function injectBluebird() {
var inject = self.options.injectBluebird;
if (!inject) {
return;
}
return HorsemanPromise.fromCallback(function hasPromise(done) {
return page.evaluate(function hasPromise() {
return (typeof window.Promise !== 'undefined');
}, done);
})
.then(function(hasPromise) {
if (hasPromise && inject !== 'bluebird') {
debug('bluebird not injected - ' +
'Promise already exists on page');
return;
}
var bbLoc = 'bluebird/js/browser/bluebird' +
(self.options.bluebirdDebug ? '' : '.min') + '.js';
return HorsemanPromise.fromCallback(function(done) {
return page.injectJs(require.resolve(bbLoc), done);
})
.tap(function(successful) {
if (!successful) {
var err = new Error('bluebird injection failed');
return HorsemanPromise.reject(err);
}
debug('injected bluebird');
});
})
.then(function configBluebird() {
return HorsemanPromise.fromCallback(function(done) {
return page.evaluate(
function configBluebird(noConflict, debug) {
if (debug) {
// TODO: Turn on warnings in bluebird 3
Promise.longStackTraces();
}
if (noConflict) {
window.Bluebird = Promise.noConflict();
}
},
inject === 'bluebird',
self.options.bluebirdDebug,
done);
});
});
})
.then(function initWindow() {
return HorsemanPromise.fromCallback(function(done) {
return page.evaluate(function initWindow() {
window.__horseman = {};
}, done);
});
})
.then(function finishLoad() {
if (page.onLoadFinished2) {
return page.onLoadFinished2.apply(page, args);
}
})
.bind(self)
.asCallback(page.loadDone);
}
}
};
//**************************************************//
// Actions
//**************************************************//
/**
* Add an action to the Horseman (and HorsemanPromise) prototype.
* @param {string} name - Name to access action with
* @param {function} action - Code for the action.
* this will be bound to the horseman instance.
*/
Horseman.registerAction = function registerAction(name, action) {
if (typeof name === 'function') {
// Work with just a named function
action = name;
name = action.name;
}
// Keep track of page counter before each action
Horseman.prototype[name] = function() {
this.lastActionPageCnt = this.curActionPageCnt;
this.curActionPageCnt = this.pageCnt;
return action.apply(this, arguments);
};
// Allow chaining actions off HorsemanPromises
HorsemanPromise.prototype[name] = function() {
var args = arguments;
return this.then(function(val) {
this.lastVal = val;
return this[name].apply(this, args);
});
};
};
/**
* Attach all of the actions.
*/
Object.keys(actions).forEach(function attachAction(name) {
Horseman.registerAction(name, actions[name]);
});
//**************************************************//
// Cleanup
//**************************************************//
Horseman.prototype.close = function() {
debug('.close().');
var self = this;
return this.ready.finally(function() {
var p;
if (!self.closed && self.phantom) {
p = HorsemanPromise.fromCallback(function(done) {
return self.phantom.exit(done);
});
}
//self.ready = HorsemanPromise.reject(new Error('phantom is closed'));
self.closed = true;
return p || HorsemanPromise.resolve();
});
};
HorsemanPromise.prototype.close = function() {
var self = this;
return this
.finally(function() {
return this.close();
}).finally(function() {
return self;
});
};
module.exports = Horseman;
module.exports.TimeoutError = HorsemanPromise.TimeoutError;