I want to write a webextension that pushes fake json content when accessing any url that matches a pattern.
I started with the MSDN doc.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/StreamFilter/ondata
code is rather straightforward
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.replace(/Example/g, 'WebExtension Example');
filter.write(encoder.encode(str));
filter.disconnect();
}
}
browser.webRequest.onBeforeRequest.addListener(
listener,
{urls: ["https://example.com/*"], types: ["main_frame"]},
["blocking"]
);
But this is not what.
I don't want to (wait for then) alter content.
I want to push my content right away.
So I changed the listener for
filter.onstart = event => {
var url = event. ???
if (url.indexOf("any string I'd like to look for"))
{
filter.write(encoder.encode("some text to display"));
filter.close();
}
else // I'm not interested in you, just process as usual
{
filter.disconnect()
}
}
My aim is to catch any url, look for a pattern in it and display my content if it matches.
Is looking for onstart the right way to do it?
If yes, I found strange than googling does not return more exemples
and I can't find doc for the event that occurs for the on start.
How is this event called? Where can I find doc for it (and events that occured ondata onclose...)?
If no,
what will be the right way for a web extension to intercept a page request to push (or not) its content instead.
Will my idea of page substitution work if I'm using Ajax/fetch to get it?
Will I be able to set the mimetype too? (I want to push json content)