If you want to intercept and modify a incoming json response for some specific url pattern, would a service worker be a good way to do so?
To illustrate, assume I frequently browse a blog and want to trick my browser into thinking that I have "favorited" every post. It's trivial to write a for loop that iterates over response.json and sets `is_favorite = true`. But it's not as clear to me where this script should ideally live in order to have the logic always executed before the response is made available to the site.
Your comment made me think about whether I can replace my overkill solution (https://requestly.io/) with something lightweight.
I don't think this will work well as a third party. Since service workers are so powerful they are quite tricky to get set up. You would need to serve it from the domain of the page that you want to intercept. You are probably much better off using a user-script or browser extension for this. I think both provide APIs for request interception but for your use case it may be easier to wrap and intercept the `fetch` or `XMLHttpRequest` API.
> If you want to intercept and modify a incoming json response for some specific url pattern, would a service worker be a good way to do so?
Yes but in this case you would need to setup a reverse proxy so your service worker and the intercepted page runs in the same domain. You could use cors-anywhere[1] for that.
> PS - Why do you think this is an overkill solution?
Only because my use case is very trivial and does not require most of the features. On the other hand, it did get the job done on the first try so I can't really complain.
To illustrate, assume I frequently browse a blog and want to trick my browser into thinking that I have "favorited" every post. It's trivial to write a for loop that iterates over response.json and sets `is_favorite = true`. But it's not as clear to me where this script should ideally live in order to have the logic always executed before the response is made available to the site.
Your comment made me think about whether I can replace my overkill solution (https://requestly.io/) with something lightweight.