let filter = browser.webRequest.filterResponseData(details.requestId);
filter.ondata = (event) => {
console.log(`filter.ondata received ${event.data.byteLength} bytes`);
filter.write(event.data);
};
filter.onstop = (event) => {
// The extension should always call filter.close() or filter.disconnect()
// after creating the StreamFilter, otherwise the response is kept alive forever.
// If processing of the response data is finished, use close. If any remaining
// response data should be processed by Firefox, use disconnect.
filter.close();
};webRequest.filterResponseData() / notebook
webRequest.filterResponseData()
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
function listener(details) {
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
filter.ondata = (event) => {
let str = decoder.decode(event.data, { stream: true });
// Just change any instance of Example in the HTTP response
// to WebExtension Example.
str = str.replaceAll("Example", "WebExtension Example");
filter.write(encoder.encode(str));
filter.disconnect();
};
return {};
}
browser.webRequest.onBeforeRequest.addListener(
listener,
{ urls: ["https://example.com/*"], types: ["main_frame"] },
["blocking"],
);