Are web extension supposed to intercept web request in scripting too?
I created a extension with the following permissions
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
]
The following background.js is working great in a browser tab
function listener(details) {
let filter = browser.webRequest.filterResponseData(details.requestId);
let url = details.url;
let method = details.method;
// for other usefull informations
// see https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest#details
filter.onstart = event => {
if (url.indexOf("foo.json") != -1)
{
console.log("started");
let encoder = new TextEncoder();
filter.write(encoder.encode(`<html><body>OK</body></html>`));
filter.close();
}
else
{
filter.disconnect();
}
}
}
browser.webRequest.onBeforeRequest.addListener(
listener,
{urls: ["<all_urls>"]},
["blocking"]
);
Working great = it serves the text "OK" if I add "foo.json" for any url on any site. This is what I expected.
But not at all in a script
fetch('foo.json').then(function(response) {
if(response.ok) {
debugger;
} else {
console.log('network exception');
}
})
.catch(function(error) {
console.log('fetch failed: ' + error.message);
});
Response is not ok : it contains the original 404 emitted by site