My goal
… is to have a web extension (in Firefox for now) that intercepts and modified XMLHttpRequests issues by a site as transparently as possible. Best case is that the extension is undetectable by the site even when explicitly looking for it. For example, I want to be able to automatically redact/replace critical data before it is send out or enforce caching of large images despite the original page disabling that explicitly.
Original approach
Use a background script and browser.webRequest
with blocking
to intercept all requests.
browser.webRequest.onBeforeRequest.addListener(
modifySend,
{
urls: ["<all_urls>"],
},
["blocking", "requestBody"]
);
browser.webRequest.onResponseStarted.addListener(
modifyReceive,
{
urls: ["<all_urls>"],
},
["blocking", "responseHeaders"]
);
This works to some degree. While I can view all requests, changing the content of sends (POST) is not possible and (some?) changed headers of received are ignored by Firefox. For example I was not able to overwrite cache-control
, the original value was still effective.
Content scripts
Since I'm only interested in XMLHttpRequests why not have a content script modify those as suggested in another question. Unfortunately, content scripts are isolated from the page they run in and injected scripts are detectable. The problem of changing response headers is also not solved.
Questions
What is the proper way to intercept and modify XMLHttpRequests? Is it even possible to the degree I want? And as an extension: How to I provide large data blobs as a response, for example if I do my own caching when I cannot persuade the browser to ignore "no caching" headers?