Skip to content

Instantly share code, notes, and snippets.

@indiejoseph
Created July 4, 2012 14:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save indiejoseph/3047593 to your computer and use it in GitHub Desktop.
Save indiejoseph/3047593 to your computer and use it in GitHub Desktop.
UserScript - XHR Interceptor
function Interceptor(nativeOpenWrapper, nativeSendWrapper) {
XMLHttpRequest.prototype.open = function () {
// Code here to intercept XHR
return nativeOpenWrapper.apply(this, arguments);
}
XMLHttpRequest.prototype.send = function () {
this.onloadend = function() {
if(this.capture) {
console.log(this.responseText);
}
}
return nativeSendWrapper.apply(this, arguments);
}
}
// Injects the code via a dynamic script tag
var script = document.createElement("script");
script.type = "text/javascript";
script.textContent = "(" + Interceptor + ")(XMLHttpRequest.prototype.open, XMLHttpRequest.prototype.send);";
document.documentElement.appendChild(script);
document.documentElement.removeChild(script);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment