3

What I want to do

I'm trying to intercept a third party website's fetch events and modify its request body in a Chrome extension. Modifying the request body is not allowed by the chrome.webRequest.onBeforeRequest event handler. But it looks like regular service workers do have the ability to listen for and modify fetch events and manually respond to the request using my own response, which means I should be able to intercept the request, modify the body, send the modified request to the API, and then respond to the original request with my modified request's response.

The problem

It looks like neither of these event handlers ever get triggered, despite plenty of fetch events being triggered by the website, as I can see in the Network panel.

// background.js

self.onfetch = (event) => console.log(event.request); // never shows

// or

self.addEventListener("fetch", (event) => {
  console.log(event.request); // never shows
});

I can verify that the service worker is running by seeing other console.logs appearing in the service worker console, both top-level logs as well as logs triggered by the "install" event

// background.js

console.log(self); // works

self.addEventListener("install", (event) => {
  console.log(event); // works
});

Hypothesis

Do the fetch event handlers not get triggered because extension service workers are not allowed access to these for security reasons? That would make sense, I just haven't seen this documented anywhere explicitly so it would be good to know if this is indeed a platform limitation or if I'm doing something wrong.

Alternate solutions?

If this is indeed a limitation of the extensions platform, is there any way other way I can use a Chrome extension to modify request bodies on a third party website?

Aron
  • 8,696
  • 6
  • 33
  • 59
  • 1
    The fetch event of your extension's service worker is only for urls like chrome-extension://your-ext-id which is not related to the web pages. The solutions can be adapted from those listed [here](https://stackoverflow.com/q/8939467) and [here](https://stackoverflow.com/q/18534771) (there may be better examples though). – wOxxOm Jul 24 '22 at 19:14
  • I'm looking to modify the request body, not read the response – Aron Jul 24 '22 at 21:20
  • Yes, as I noted, these examples can be adapted. The approach is the same. – wOxxOm Jul 24 '22 at 21:33
  • IIRC, xhook.js library offers all of that. – wOxxOm Jul 24 '22 at 21:35
  • I'm pretty sure those don't work when used in Chrome extensions. I tried monkeypatching `window.fetch` - which appears to be how xhook.js works - from both content scripts and service workers and nothing happened – Aron Jul 24 '22 at 21:38
  • Those examples are explicitly made to work in chrome extensions. To use xhook.js you need to run it in the [page context](/a/9517879) (aka "main world") similarly to the examples. – wOxxOm Jul 24 '22 at 21:57
  • Is fetch event handler invoked for the same origin requests? – Žilvinas Rudžionis Jul 20 '23 at 12:32

0 Answers0