function listener(details) {
const filter = browser.webRequest.filterResponseData(details.requestId);
const encoder = new TextEncoder();
filter.ondata = (event) => {
// pass through all the response data
filter.write(event.data);
};
filter.onstop = (event) => {
filter.write(encoder.encode("extra stuff"));
filter.disconnect();
};
}
browser.webRequest.onBeforeRequest.addListener(
listener,
{ urls: ["https://example.com/*"], types: ["main_frame"] },
["blocking"],
);webRequest.StreamFilter.onstop / notebook
webRequest.StreamFilter.onstop
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
function listener(details) {
const filter = browser.webRequest.filterResponseData(details.requestId);
const encoder = new TextEncoder();
const data = [];
filter.ondata = (event) => {
data.push(event.data);
};
filter.onstop = (event) => {
for (const buffer of data) {
filter.write(buffer);
}
filter.write(encoder.encode("extra stuff"));
filter.close();
};
}
browser.webRequest.onBeforeRequest.addListener(
listener,
{ urls: ["https://example.com/"], types: ["main_frame"] },
["blocking"],
);