Skip to content

Instantly share code, notes, and snippets.

@gllist
Forked from markstreich/gist:3667944
Created September 7, 2012 17:36
Show Gist options
  • Save gllist/3668037 to your computer and use it in GitHub Desktop.
Save gllist/3668037 to your computer and use it in GitHub Desktop.
PhantomJS: Ajax response capture through console.log
var page = require('webpage').create();
page.open('http://www.website.com/', function (status) {
if (status === 'success') {
captureAjaxResponsesToConsole();
}
});
function captureAjaxResponsesToConsole() {
// logs ajax response contents to console so sublime's onConsoleMessage can use the contents
// credit to Ionuț G. Stan
// http://stackoverflow.com/questions/629671/how-can-i-intercept-xmlhttprequests-from-a-greasemonkey-script
page.evaluate(function() {
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener("readystatechange", function() {
if (this.readyState === 4) {
var res={'response':this.responseText, 'url':url};
console.log(JSON.stringify(res));
}
}, false);
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
return 1;
});
}
page.onConsoleMessage = function (msg) {
var res=JSON.parse(msg);
console.log('--------------------');
console.log('URL:' + res.url);
console.log('Response: ' + res.response);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment