UPDATE: I realize now that in the scenario described below I will need to be careful of infinite loops since the page being redirected to is the same (minus the added param) as the initial page. Does anyone have any experience with that in Webextensions and can offer any advice on how best to avoid these loops?
I've written a few Webextensions for Firefox before but it's been a few years (a little out of practise). But a new problem has arisen for which I can see no solution except to write my own Webextension.
How would I modify the URL of a page and add a query parameter? I am specifically trying to modify the URL of Google Docs pages and add the query param ?mode=html
So for example, a Google Docs URL might look like: https://docs.google.com/document/d/7Ujfjsd69To7lInXFOdn49nAxLLhfn43fj43JHDTp87/edit
I am looking to create an extension that will modify that URL so it becomes: https://docs.google.com/document/d/7Ujfjsd69To7lInXFOdn49nAxLLhfn43fj43JHDTp87/edit?mode=html
(Obviously I only want this param appended when the URL being loaded matches https://docs.google.com/document/d/*)
Can anyone give me a pointer on how to start that? What extensionAPI function(s) would I need to use to do that? At what point in the lifespan of the request should it happen etc?
Note: requires "webRequest" & "webRequestBlocking" permission plus host permission
// add a listener for https://docs.google.com/document/*
browser.webRequest.onBeforeRequest.addListener(process, {
urls: ['https://docs.google.com/document/*'],
types: ['main_frame']
},
['blocking']
);
function process(details) {
// check it doesn't have mode=html
if (!details.url.includes('mode=html')) {
// add &mode=html if URL already has search string, otherwsie ?mode=html
return {redirectUrl: details.url + (details.url.includes('?') ? '&' : '?') + 'mode=html'};
}
}