Add the background script.

This commit is contained in:
Bauke 2022-10-14 18:15:04 +02:00
parent 4cbe31c63a
commit 263c0497f3
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import browser from 'webextension-polyfill';
import {parseRedirect} from '../redirect/exports.js';
async function browserActionClicked() {
await browser.runtime.openOptionsPage();
}
if (import.meta.env.VITE_BROWSER === 'chromium') {
browser.action.onClicked.addListener(browserActionClicked);
} else {
browser.browserAction.onClicked.addListener(browserActionClicked);
}
browser.runtime.onInstalled.addListener(async () => {
if (import.meta.env.DEV) {
await browser.runtime.openOptionsPage();
}
});
browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
const url = new URL(details.url);
for (const parameters of Object.values(await browser.storage.local.get())) {
const redirect = parseRedirect(parameters);
if (redirect === undefined) {
continue;
}
if (redirect.isMatch(url)) {
const redirectedUrl = redirect.redirect(url);
await browser.tabs.update({url: redirectedUrl.href});
break;
}
}
});