Last active
September 22, 2021 08:14
-
-
Save oksuz/2d2f0852b6ffe906a82886ef59d507e8 to your computer and use it in GitHub Desktop.
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
const headerStorage = {}; // keeps request headers | |
function interceptXHR(interceptor) { | |
const open = XMLHttpRequest.prototype.open; | |
const send = XMLHttpRequest.prototype.send; | |
const setRequestHeader = XMLHttpRequest.prototype.setRequestHeader; | |
XMLHttpRequest.prototype.setRequestHeader = function (header, value) { | |
headerStorage[header] = value; | |
setRequestHeader.call(this, header, value); | |
}; | |
XMLHttpRequest.prototype.open = function ( | |
method, | |
url, | |
_async, | |
user, | |
password | |
) { | |
this._requestUrl = url; | |
open.call(this, method, url, _async, user, password); | |
}; | |
XMLHttpRequest.prototype.send = function (data) { | |
this.addEventListener( | |
"readystatechange", | |
(ev) => { | |
interceptor(this, ev); | |
}, | |
false | |
); | |
send.call(this, data); | |
}; | |
} | |
/** | |
interceptXHR((request, event) => { | |
// triggers on readystatechange | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment