Skip to content

Instantly share code, notes, and snippets.

@oksuz
Last active September 22, 2021 08:14
Show Gist options
  • Save oksuz/2d2f0852b6ffe906a82886ef59d507e8 to your computer and use it in GitHub Desktop.
Save oksuz/2d2f0852b6ffe906a82886ef59d507e8 to your computer and use it in GitHub Desktop.
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