2022-10-14 16:15:04 +00:00
|
|
|
import browser from 'webextension-polyfill';
|
|
|
|
|
2022-10-27 19:13:02 +00:00
|
|
|
import storage from '../redirect/storage.js';
|
2022-10-14 16:15:04 +00:00
|
|
|
|
|
|
|
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) => {
|
2022-11-02 11:16:39 +00:00
|
|
|
if (!details.url.startsWith('http') || details.frameId > 0) {
|
2022-10-22 15:42:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-21 16:49:16 +00:00
|
|
|
const {latestTime} = await browser.storage.local.get('latestTime');
|
|
|
|
const redirectDelta = Date.now() - (latestTime ?? 0);
|
|
|
|
if (redirectDelta < 100) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:15:04 +00:00
|
|
|
const url = new URL(details.url);
|
2022-10-21 16:49:16 +00:00
|
|
|
const {latestUrl} = await browser.storage.local.get('latestUrl');
|
|
|
|
if (redirectDelta < 30_000 && url.href === latestUrl) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-14 16:15:04 +00:00
|
|
|
|
2022-10-27 19:13:02 +00:00
|
|
|
for (const redirect of await storage.getRedirects()) {
|
|
|
|
if (!redirect.parameters.enabled) {
|
2022-10-14 16:15:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirect.isMatch(url)) {
|
|
|
|
const redirectedUrl = redirect.redirect(url);
|
2022-10-26 16:00:42 +00:00
|
|
|
await browser.tabs.update(details.tabId, {url: redirectedUrl.href});
|
2022-10-21 16:49:16 +00:00
|
|
|
await browser.storage.local.set({
|
|
|
|
latestTime: Date.now(),
|
|
|
|
latestUrl: url.href,
|
|
|
|
});
|
2022-10-14 16:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|