xhr-sniffer
Version:
🔨 Sniff HTTP requests making by XHR in the browser or HTTP module in Node.js
43 lines (33 loc) • 1.15 kB
JavaScript
;
(function (XMLHttpRequest) {
const MAX_HTTP_METHOD_NAME = 6;
const send = XMLHttpRequest.prototype.send;
const open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
const [method, url] = arguments;
this.method = method;
this.requestURL = url;
return open.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function () {
const self = this;
const startRequestTime = Date.now();
this.addEventListener('error', (err) => {
console.error(err);
});
this.addEventListener('loadend', () => {
const duration = Date.now() - startRequestTime;
const url = self.requestURL;
const status = self.statusText;
const method = self.method.padEnd(MAX_HTTP_METHOD_NAME);
console.log(window.formatter({
label: 'XMLHttpRequest',
method,
status,
url,
duration
}));
});
return send.apply(this, arguments);
};
})(window.XMLHttpRequest);