From f1dcdb63aebeb6393efc41eb1c8dbb86e3d231c1 Mon Sep 17 00:00:00 2001 From: deguotaoma Date: Wed, 23 Jul 2014 10:42:37 +0200 Subject: [PATCH 1/4] add timeout for waitForPage --- lib/jquery.go.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/jquery.go.js b/lib/jquery.go.js index f6ffc0f..28e2ed7 100644 --- a/lib/jquery.go.js +++ b/lib/jquery.go.js @@ -10,6 +10,7 @@ var page = null; var pageQueue = []; var instance = null; var loading = true; +var visitStart = 0; // Create the phantom connection. phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { @@ -28,7 +29,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); @@ -52,6 +54,7 @@ phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { // Trigger when the loading has started. page.set('onLoadStarted', function() { loading = true; + ready = false; }); // Trigger when the loading has finished. @@ -335,7 +338,8 @@ 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) { + visitStart = Date.now(); var self = this; getPage(function(page) { @@ -365,7 +369,7 @@ var jQuery = _.extend(function(selector, context) { loadJS(); } else { - self.waitForPage(callback); + self.waitForPage(callback, false, timeout); } }); }); @@ -377,15 +381,16 @@ var jQuery = _.extend(function(selector, context) { * @param {type} callback * @returns {undefined} */ - waitForPage: function(callback, nowait) { + waitForPage: function(callback, nowait, timeout) { var self = this; + var remain = (Date.now() - visitStart) - timeout; var loadWait = function() { setTimeout(function() { - self.waitForPage(callback, true); + self.waitForPage(callback, true, timeout); }, 100); }; if (nowait) { - if (loading) { + if (loading && remain <= 0 && !ready) { loadWait(); } else { @@ -416,7 +421,7 @@ var jQuery = _.extend(function(selector, context) { if (!nowait) { this.waitForPage(function() { self.waitForElement(element, callback, true); - }); + }, nowait, -1); } else { var loadWait = function() { From 78218c699b72c595d4148d12b99247055ad8c450 Mon Sep 17 00:00:00 2001 From: deguotaoma Date: Wed, 23 Jul 2014 12:22:01 +0200 Subject: [PATCH 2/4] fix --- lib/jquery.go.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/jquery.go.js b/lib/jquery.go.js index 28e2ed7..fdc4b7d 100644 --- a/lib/jquery.go.js +++ b/lib/jquery.go.js @@ -10,7 +10,7 @@ var page = null; var pageQueue = []; var instance = null; var loading = true; -var visitStart = 0; + // Create the phantom connection. phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { @@ -53,8 +53,7 @@ phantom.create("--web-security=no", "--ignore-ssl-errors=yes", function(ph) { // Trigger when the loading has started. page.set('onLoadStarted', function() { - loading = true; - ready = false; + loading = true; }); // Trigger when the loading has finished. @@ -338,9 +337,9 @@ var jQuery = _.extend(function(selector, context) { * @param {function} callback * Called when the page is done visiting. */ - visit: function(url, callback, timeout) { - visitStart = Date.now(); + visit: function(url, callback, timeout) { var self = this; + self.visitStart = Date.now(); getPage(function(page) { // Set the page size if it hasn't already been set. @@ -383,7 +382,8 @@ var jQuery = _.extend(function(selector, context) { */ waitForPage: function(callback, nowait, timeout) { var self = this; - var remain = (Date.now() - visitStart) - timeout; + if(timeout===undefined) timeout = -1; + var remain = (Date.now() - self.visitStart) - timeout; var loadWait = function() { setTimeout(function() { self.waitForPage(callback, true, timeout); From c5314047931991357ce3e711f1319567f9dadb47 Mon Sep 17 00:00:00 2001 From: deguotaoma Date: Wed, 23 Jul 2014 15:30:47 +0200 Subject: [PATCH 3/4] waitForPage & visit --- lib/jquery.go.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/jquery.go.js b/lib/jquery.go.js index fdc4b7d..7af361e 100644 --- a/lib/jquery.go.js +++ b/lib/jquery.go.js @@ -339,7 +339,9 @@ var jQuery = _.extend(function(selector, context) { */ 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. @@ -358,16 +360,19 @@ 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.visitStart = Date.now(); self.waitForPage(callback, false, timeout); } }); @@ -381,19 +386,21 @@ var jQuery = _.extend(function(selector, context) { * @returns {undefined} */ waitForPage: function(callback, nowait, timeout) { - var self = this; + var self = this; if(timeout===undefined) timeout = -1; - var remain = (Date.now() - self.visitStart) - timeout; + var remain = timeout==-1? 1: (timeout - (Date.now()-self.visitStart)); + var loadWait = function() { setTimeout(function() { self.waitForPage(callback, true, timeout); }, 100); }; if (nowait) { - if (loading && remain <= 0 && !ready) { + if (loading && remain > 0 ) { loadWait(); } else { + loading = false; getPage(function(page) { page.evaluate(function() { return jQuery.isReady; From e8d035ed766beca7821f96f368e990d763321bf5 Mon Sep 17 00:00:00 2001 From: deguotaoma Date: Thu, 21 Aug 2014 01:04:14 +0200 Subject: [PATCH 4/4] Update jquery.go.js +closePage --- lib/jquery.go.js | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/jquery.go.js b/lib/jquery.go.js index 7af361e..b06ee03 100644 --- a/lib/jquery.go.js +++ b/lib/jquery.go.js @@ -11,16 +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; @@ -66,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 }); /** @@ -78,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; } }; @@ -503,6 +521,7 @@ var jQuery = _.extend(function(selector, context) { * The getPage method. */ getPage: getPage, + closePage: closePage, /** * The go method.