Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 51 additions & 20 deletions lib/jquery.go.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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 });

/**
Expand All @@ -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;
}
};

Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
});
});
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -491,6 +521,7 @@ var jQuery = _.extend(function(selector, context) {
* The getPage method.
*/
getPage: getPage,
closePage: closePage,

/**
* The go method.
Expand Down