Skip to content

Instantly share code, notes, and snippets.

@wyvernzora
Last active June 23, 2023 09:58
Show Gist options
  • Save wyvernzora/b6a73444a597011b7dec to your computer and use it in GitHub Desktop.
Save wyvernzora/b6a73444a597011b7dec to your computer and use it in GitHub Desktop.
Browser XHR Spy
/**
* The following code, when executed in a browser's JS console,
* will log detailed information about every XHR made from the
* web page. Perfect for spying on API calls.
*/
(function() {
var XHR = XMLHttpRequest.prototype;
// Remember references to original methods
var open = XHR.open;
var send = XHR.send;
// Overwrite native methods
// Collect data:
XHR.open = function(method, url) {
this._method = method;
this._url = url;
return open.apply(this, arguments);
};
// Implement "ajaxSuccess" functionality
XHR.send = function(postData) {
this.addEventListener('load', function() {
this._body = postData;
console.log(this);
});
return send.apply(this, arguments);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment