I have been attempting to create a background extension to redirect an HTTP request URL to a target URL that is the original URL + an appended string, but have been running into a few issues and the documentation I have found has not been super helpful.
So far, I managed to get the URL to automatically redirect to the target URL when I do my test as intended as it successfully transforms my URL:
- from
https://www.ecosia.org/search?method=index&q=test
- to
https://www.ecosia.org/search?q=test%20test
But the redirect greets me with this error:
I have been following this tutorial and webRequest.onBeforeRequest
documentation to learn how to implement this feature, and have come up with the following code.
background.js:
window.onload = async () => {
await browser.tabs.executeScript({file: "/lib/browser-polyfill.min.js"});
};
let pattern = "https://www.ecosia.org/search?q=test*"
const targetUrl = "https://www.ecosia.org/search?q=test duck animal"
function redirect(requestDetails) {
console.log(`Redirecting: ${requestDetails.url}`);
if (requestDetails.url === targetUrl) {
return;
}
return {
redirectUrl: targetUrl
};
}
browser.webRequest.onBeforeRequest.addListener(
redirect,
{urls:[pattern], types:["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]}, //main_frame - others
["blocking"]
);
manifest.json:
{
"permissions": [
"webRequest",
"webRequestBlocking",
"https://www.ecosia.org/"
],
"background": {
"scripts": [
"background.js"
]
}
}
Any idea what I’m doing wrong that is causing the redirect to fail? Any help would be appreciated.
Thanks!