Skip to content

Instantly share code, notes, and snippets.

@lguzzon
Forked from markstreich/gist:3667944
Last active December 20, 2015 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lguzzon/6163037 to your computer and use it in GitHub Desktop.
Save lguzzon/6163037 to your computer and use it in GitHub Desktop.
PhantomJS script that is working for me ... the up-stream no
var page = require('webpage').create();
page.onInitialized = function () {
captureAjaxResponsesToConsole();
};
page.open('http://www.website.com/', function (status) {
if (status === 'success') {
phantom.exit();
} else {
phantom.exit(1);
}
});
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