Search code examples
javascriptxmlhttprequest

How to catch and override every XHR request inclusive Native requests


Traditional solutions catch only xhr in user js. I tried Xhr.prototype override , it doesnt effect native xhr request.

I want to catch every all xhr request(all fetch api request) [Every request that seen in chrome devtool network tab] after dom loaded.

More example: <video src="aaa.mp4"> //browser native request for video and we see in the devtool Though I set the window.XMLHttpRequest = undefined , browser can request . But I cannot xhr request due to undefined.

If I override XHR , I can generate custom response without ask to server.


Solution

  • The browser's own network requests aren't made through XHR at all.

    If your goal is to intercept all of the browser's network requests for the page, you'd need to use a service worker to do that (spec | MDN).