Monkey patch XMLHttpRequest.
const xhrLogs: any[] = [];
monkeyPatchXMLHttpRequest(xhrLogs);
function monkeyPatchXMLHttpRequest (xhrLogs: any[]): void {
const open: any = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method: string, url: string, async?: boolean, username?: string, password?: string): void {
xhrLogs.push({method: method, requestURL: url});
open.apply(this, arguments);
};
const send: any = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function (data): void {
const logIndex: number = xhrLogs.length - 1;
xhrLogs[logIndex].date = new Date();
xhrLogs[logIndex].data = data;
const self: any = this;
const onreadystatechange: any = self.onreadystatechange;
if (onreadystatechange) {
self.onreadystatechange = function () {
xhrLogs[logIndex].readyState = self.readyState;
if (self.readyState === 4) {
xhrLogs[logIndex].url = self.responseURL;
xhrLogs[logIndex].readyState = 4;
xhrLogs[logIndex].responseHeaders = self.getAllResponseHeaders();
xhrLogs[logIndex].status = self.status;
xhrLogs[logIndex].statusText = self.statusText;
xhrLogs[logIndex].responseText = self.responseText.indexOf('PNG') !== -1 ? 'Картинка PNG' : self.responseText;
}
onreadystatechange.apply(self, arguments);
}
}
send.apply(self, arguments);
};
}