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-11-15 11:46:10 +00:00
|
|
|
import {updateBadge} from '../utilities/badge.js';
|
2022-11-21 12:25:00 +00:00
|
|
|
import {onCommandsHandler} from './commands.js';
|
2022-11-15 11:46:10 +00:00
|
|
|
import {
|
|
|
|
contextClicked,
|
|
|
|
getContextMenus,
|
|
|
|
initializeContextMenus,
|
|
|
|
} from './context-menus.js';
|
2022-10-14 16:15:04 +00:00
|
|
|
|
|
|
|
async function browserActionClicked() {
|
|
|
|
await browser.runtime.openOptionsPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
browser.runtime.onInstalled.addListener(async () => {
|
2022-11-15 11:46:10 +00:00
|
|
|
await initializeContextMenus();
|
|
|
|
await updateBadge();
|
|
|
|
|
2022-10-14 16:15:04 +00:00
|
|
|
if (import.meta.env.DEV) {
|
|
|
|
await browser.runtime.openOptionsPage();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-15 11:46:10 +00:00
|
|
|
browser.runtime.onStartup.addListener(async () => {
|
|
|
|
await updateBadge();
|
|
|
|
});
|
|
|
|
|
2022-10-14 16:15:04 +00:00
|
|
|
browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
|
2022-11-15 11:56:27 +00:00
|
|
|
const {redirectsEnabled} = await browser.storage.local.get({
|
|
|
|
redirectsEnabled: true,
|
|
|
|
});
|
|
|
|
if (redirectsEnabled === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-11-15 11:24:44 +00:00
|
|
|
const tab = await browser.tabs.query({active: true, lastFocusedWindow: true});
|
|
|
|
const currentTabUrl =
|
|
|
|
tab[0]?.url === undefined ? undefined : new URL(tab[0].url);
|
|
|
|
|
2022-10-14 16:15:04 +00:00
|
|
|
const url = new URL(details.url);
|
2022-11-21 13:50:55 +00:00
|
|
|
|
|
|
|
// The undefined.local URL will only be used if no redirects have happened yet.
|
|
|
|
const {latestUrl: savedLatestUrl} = await browser.storage.local.get({
|
|
|
|
latestUrl: 'https://undefined.local',
|
|
|
|
});
|
|
|
|
|
|
|
|
const latestUrl = new URL(savedLatestUrl);
|
|
|
|
|
|
|
|
// Set the latest URL protocol to always be the same as the current. Since
|
|
|
|
// only HTTP URLs are checked here, for us HTTP and HTTPS are equivalent.
|
|
|
|
latestUrl.protocol = url.protocol;
|
|
|
|
|
|
|
|
const currentUrlWwwPrefix = url.hostname.startsWith('www.');
|
|
|
|
const latestUrlWwwPrefix = latestUrl.hostname.startsWith('www.');
|
|
|
|
if (currentUrlWwwPrefix && !latestUrlWwwPrefix) {
|
|
|
|
// Then if the current URL is a `www.` URL and the latest one isn't, prefix it
|
|
|
|
// to the latest URL. This helps the manual bypass check.
|
|
|
|
latestUrl.hostname = `www.${latestUrl.hostname}`;
|
|
|
|
} else if (!currentUrlWwwPrefix && latestUrlWwwPrefix) {
|
|
|
|
// Remove `www.` if the latestUrl starts with it but the current URL doesn't.
|
|
|
|
latestUrl.hostname = latestUrl.hostname.slice(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually bypass any redirects if the latest redirected and current URLs are
|
|
|
|
// the same.
|
|
|
|
if (redirectDelta < 30_000 && url.href === latestUrl.href) {
|
2022-10-21 16:49:16 +00:00
|
|
|
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)) {
|
2022-11-15 11:24:44 +00:00
|
|
|
// Don't redirect if the URL before going to a new page is also a match.
|
|
|
|
// This will happen when the user is already on a website that has a
|
|
|
|
// redirect, but for whatever reason hasn't redirected. So it's safe to
|
|
|
|
// assume that they want to stay on this website, rather than get
|
|
|
|
// redirected.
|
|
|
|
if (currentTabUrl !== undefined && redirect.isMatch(currentTabUrl)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:43:56 +00:00
|
|
|
let redirectedUrl = redirect.redirect(url);
|
|
|
|
if (typeof redirectedUrl === 'string') {
|
|
|
|
try {
|
|
|
|
redirectedUrl = new URL(redirectedUrl);
|
|
|
|
} catch {
|
|
|
|
redirectedUrl = `https://${redirectedUrl as string}`;
|
|
|
|
redirectedUrl = new URL(redirectedUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-11-15 11:46:10 +00:00
|
|
|
|
|
|
|
browser.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
|
const contextMenus = getContextMenus();
|
|
|
|
const contextMenuIds = new Set<string>(
|
|
|
|
contextMenus.map(({id}) => id ?? 're-nav-unknown'),
|
|
|
|
);
|
|
|
|
|
|
|
|
await contextClicked(contextMenuIds, info, tab);
|
|
|
|
});
|
2022-11-16 11:15:04 +00:00
|
|
|
|
2022-11-21 12:25:00 +00:00
|
|
|
browser.commands.onCommand.addListener(onCommandsHandler);
|
|
|
|
|
2022-11-16 11:15:04 +00:00
|
|
|
if (import.meta.env.VITE_BROWSER === 'chromium') {
|
|
|
|
browser.action.onClicked.addListener(browserActionClicked);
|
|
|
|
} else {
|
|
|
|
browser.browserAction.onClicked.addListener(browserActionClicked);
|
|
|
|
void initializeContextMenus();
|
|
|
|
}
|