diff --git a/lib/jquery.go.js b/lib/jquery.go.js index f6ffc0f..b06ee03 100644 --- a/lib/jquery.go.js +++ b/lib/jquery.go.js @@ -11,15 +11,8 @@ var pageQueue = []; var instance = null; var loading = true; -// Create the phantom connection. -phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { - - // Save the instance. - instance = ph; - - // Create the page. +var createPage = function(ph){ return ph.createPage(function(pg) { - // Set the page. page = pg; @@ -28,7 +21,8 @@ phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { // Whether page has loaded. var ready = false; - + + // Pass along console messages. page.set('onConsoleMessage', function(msg) { console.log('Console:' + msg); @@ -51,7 +45,7 @@ phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { // Trigger when the loading has started. page.set('onLoadStarted', function() { - loading = true; + loading = true; }); // Trigger when the loading has finished. @@ -64,7 +58,24 @@ phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { _.each(pageQueue, function(queue) { queue(page); }); + pageQueue = []; }); +}; + + +// Create the phantom connection. +phantom.create( + "--web-security=no", + "--ignore-ssl-errors=yes", + "--load-images=false", + function(ph) { + + // Save the instance. + instance = ph; + + // Create the page. + return createPage(ph); + }, { phantomPath: phantomjs.path }); /** @@ -76,9 +87,18 @@ phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { var getPage = function(callback) { if (page) { callback(page); - } - else { + } else { pageQueue.push(callback); + if( instance ) { + createPage(instance); + } + } +}; + +var closePage = function(){ + if(page){ + page.close(); + page = null; } }; @@ -335,8 +355,11 @@ var jQuery = _.extend(function(selector, context) { * @param {function} callback * Called when the page is done visiting. */ - visit: function(url, callback) { + visit: function(url, callback, timeout) { var self = this; + if(timeout===undefined) timeout = -1; + self.visitStart = Date.now(); + getPage(function(page) { // Set the page size if it hasn't already been set. @@ -355,17 +378,20 @@ var jQuery = _.extend(function(selector, context) { page.open(self.config.site + url, function() { if (self.config.addJQuery) { var loadJS = function() { - if (loading) { + var remain = timeout==-1? 1: (timeout - (Date.now()-self.visitStart)) ; + if (loading && remain> 0 ) { setTimeout(loadJS, 100); } else { + loading = false; page.includeJs(self.config.jQuery, callback); } } loadJS(); } else { - self.waitForPage(callback); + self.visitStart = Date.now(); + self.waitForPage(callback, false, timeout); } }); }); @@ -377,18 +403,22 @@ var jQuery = _.extend(function(selector, context) { * @param {type} callback * @returns {undefined} */ - waitForPage: function(callback, nowait) { - var self = this; + waitForPage: function(callback, nowait, timeout) { + var self = this; + if(timeout===undefined) timeout = -1; + var remain = timeout==-1? 1: (timeout - (Date.now()-self.visitStart)); + var loadWait = function() { setTimeout(function() { - self.waitForPage(callback, true); + self.waitForPage(callback, true, timeout); }, 100); }; if (nowait) { - if (loading) { + if (loading && remain > 0 ) { loadWait(); } else { + loading = false; getPage(function(page) { page.evaluate(function() { return jQuery.isReady; @@ -416,7 +446,7 @@ var jQuery = _.extend(function(selector, context) { if (!nowait) { this.waitForPage(function() { self.waitForElement(element, callback, true); - }); + }, nowait, -1); } else { var loadWait = function() { @@ -491,6 +521,7 @@ var jQuery = _.extend(function(selector, context) { * The getPage method. */ getPage: getPage, + closePage: closePage, /** * The go method.