lib/index.js
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);
		}
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);
		}
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 ) ; }
Do any javascript injection on the page
TODO: Consolidate into one page.evaluate?
Do any javascript injection on the page TODO : Consolidate into one page . evaluate?