Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event listener for window.fetch

Is there any possibility to globally monitor all window.fetch requests? I know it works with Promises, which is great, but I need to get notified whenever any of such requests is done (fail/success). I have no access to the functions which invoke these or are success/fail handlers.

So basically I'm looking for something like

window.addEventListener('fetch', ev => {
  if (ev.type == 'FetchSuccess') {
    // process ev.response
  }
});
like image 347
may.tau Avatar asked Sep 23 '16 16:09

may.tau


1 Answers

You can overwrite the fetch function the following way:

var oldFetch = fetch;  // must be on the global scope
fetch = function(url, options) {
    var promise = oldFetch(url, options);
    // Do something with the promise
    return promise;
}

This way you can get control over the returned promises while it maintains it's usage.

like image 149
Bálint Avatar answered Sep 24 '22 17:09

Bálint