Created
April 14, 2017 01:23
-
-
Save stemwinder/6fae2ec13939eaa662b2a5af4f3949ba to your computer and use it in GitHub Desktop.
Injectable JS to capture XHR responses
This file contains hidden or 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
// http://stackoverflow.com/a/10796951 | |
(function(XHR) { | |
"use strict"; | |
var element = document.createElement('div'); | |
element.id = "interceptedResponse"; | |
element.appendChild(document.createTextNode("")); | |
document.body.appendChild(element); | |
var open = XHR.prototype.open; | |
var send = XHR.prototype.send; | |
XHR.prototype.open = function(method, url, async, user, pass) { | |
this._url = url; // want to track the url requested | |
open.call(this, method, url, async, user, pass); | |
}; | |
XHR.prototype.send = function(data) { | |
var self = this; | |
var oldOnReadyStateChange; | |
var url = this._url; | |
function onReadyStateChange() { | |
if(self.status === 200 && self.readyState == 4 /* complete */) { | |
document.getElementById("interceptedResponse").innerHTML += | |
'{"url":"' + url + '", "data":' + self.responseText + '}*****'; | |
} | |
if(oldOnReadyStateChange) { | |
oldOnReadyStateChange(); | |
} | |
} | |
if(this.addEventListener) { | |
this.addEventListener("readystatechange", onReadyStateChange, | |
false); | |
} else { | |
oldOnReadyStateChange = this.onreadystatechange; | |
this.onreadystatechange = onReadyStateChange; | |
} | |
send.call(this, data); | |
} | |
})(XMLHttpRequest); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work