Skip to content

Instantly share code, notes, and snippets.

@ii0
Forked from gllist/gist:3668037
Created March 26, 2019 13:37
Show Gist options
  • Save ii0/7f4cd0d3e044a8d4fbe9941b4efa4754 to your computer and use it in GitHub Desktop.
Save ii0/7f4cd0d3e044a8d4fbe9941b4efa4754 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