Listening to AJAX events at a global level
suggest changeThis patches XMLHttpRequest. It does not see fetch. The listener used to log XHR, which is not defined and throws ReferenceError when the load event runs. Each call to open adds another listener, so a reused object logs more than once.
// Store a reference to the native method
let open = XMLHttpRequest.prototype.open;
// Overwrite the native method
XMLHttpRequest.prototype.open = function() {
this.addEventListener("load", event => console.log(event.target), false);
open.apply(this, arguments);
};
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents