-
-
Save gllist/3668037 to your computer and use it in GitHub Desktop.
PhantomJS: Ajax response capture through console.log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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