Listening to AJAX events at a global level

suggest change

This 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);
};

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents