Compare commits

..

No commits in common. "bb806cb5615b866d15fcfe4cbb0e60df7f7f38f6" and "6ab041df93b6bcf0763a90ba8e0c0448aeb82be9" have entirely different histories.

10 changed files with 15 additions and 74 deletions

View File

@ -1,7 +0,0 @@
import {toggleAllRedirects} from '../utilities/toggle-all-redirects.js';
export async function onCommandsHandler(command: string): Promise<void> {
if (command === 'toggleAllRedirects') {
await toggleAllRedirects();
}
}

View File

@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'; import browser from 'webextension-polyfill';
import {toggleAllRedirects} from '../utilities/toggle-all-redirects.js'; import {updateBadge} from '../utilities/badge.js';
type ContextMenu = browser.Menus.CreateCreatePropertiesType; type ContextMenu = browser.Menus.CreateCreatePropertiesType;
@ -48,6 +48,9 @@ export async function contextClicked(
} }
if (id === 're-nav-toggle-redirects') { if (id === 're-nav-toggle-redirects') {
await toggleAllRedirects(); const state = await browser.storage.local.get({redirectsEnabled: true});
const redirectsEnabled = !(state.redirectsEnabled as boolean);
await browser.storage.local.set({redirectsEnabled});
await updateBadge(redirectsEnabled);
} }
} }

View File

@ -2,7 +2,6 @@ import browser from 'webextension-polyfill';
import storage from '../redirect/storage.js'; import storage from '../redirect/storage.js';
import {updateBadge} from '../utilities/badge.js'; import {updateBadge} from '../utilities/badge.js';
import {onCommandsHandler} from './commands.js';
import { import {
contextClicked, contextClicked,
getContextMenus, getContextMenus,
@ -49,32 +48,8 @@ browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
tab[0]?.url === undefined ? undefined : new URL(tab[0].url); tab[0]?.url === undefined ? undefined : new URL(tab[0].url);
const url = new URL(details.url); const url = new URL(details.url);
const {latestUrl} = await browser.storage.local.get('latestUrl');
// The undefined.local URL will only be used if no redirects have happened yet. if (redirectDelta < 30_000 && url.href === latestUrl) {
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) {
return; return;
} }
@ -93,16 +68,7 @@ browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
break; break;
} }
let redirectedUrl = redirect.redirect(url); const redirectedUrl = redirect.redirect(url);
if (typeof redirectedUrl === 'string') {
try {
redirectedUrl = new URL(redirectedUrl);
} catch {
redirectedUrl = `https://${redirectedUrl as string}`;
redirectedUrl = new URL(redirectedUrl);
}
}
await browser.tabs.update(details.tabId, {url: redirectedUrl.href}); await browser.tabs.update(details.tabId, {url: redirectedUrl.href});
await browser.storage.local.set({ await browser.storage.local.set({
latestTime: Date.now(), latestTime: Date.now(),
@ -122,8 +88,6 @@ browser.contextMenus.onClicked.addListener(async (info, tab) => {
await contextClicked(contextMenuIds, info, tab); await contextClicked(contextMenuIds, info, tab);
}); });
browser.commands.onCommand.addListener(onCommandsHandler);
if (import.meta.env.VITE_BROWSER === 'chromium') { if (import.meta.env.VITE_BROWSER === 'chromium') {
browser.action.onClicked.addListener(browserActionClicked); browser.action.onClicked.addListener(browserActionClicked);
} else { } else {

View File

@ -12,15 +12,6 @@ export default function createManifest(
page: 'options/index.html', page: 'options/index.html',
open_in_tab: true, open_in_tab: true,
}, },
commands: {
toggleAllRedirects: {
description:
"Toggle all redirects, this does the same as the extension icon's right-click option.",
suggested_key: {
default: 'Alt+Shift+R',
},
},
},
}; };
const icons = { const icons = {

View File

@ -48,5 +48,5 @@ export abstract class Redirect {
return false; return false;
} }
public abstract redirect(url: URL | string): URL | string; public abstract redirect(url: URL | string): URL;
} }

View File

@ -8,7 +8,7 @@ export * from './hostname.js';
export * from './regex.js'; export * from './regex.js';
export * from './simple.js'; export * from './simple.js';
export type Redirects = HostnameRedirect | RegexRedirect | SimpleRedirect; export type Redirects = HostnameRedirect | SimpleRedirect;
export function parseRedirect( export function parseRedirect(
parameters: RedirectParameters, parameters: RedirectParameters,

View File

@ -1,9 +1,9 @@
import {Redirect} from './base.js'; import {Redirect} from './base.js';
export class RegexRedirect extends Redirect { export class RegexRedirect extends Redirect {
public redirect(redirect: URL | string): string { public redirect(redirect: URL | string): URL {
const url = redirect instanceof URL ? redirect.href : redirect; const url = redirect instanceof URL ? redirect.href : redirect;
const regex = new RegExp(this.parameters.matcherValue, 'gi'); const regex = new RegExp(this.parameters.matcherValue, 'gi');
return url.replace(regex, this.parameters.redirectValue); return new URL(url.replace(regex, this.parameters.redirectValue));
} }
} }

View File

@ -1,7 +1,7 @@
import {Redirect} from './base.js'; import {Redirect} from './base.js';
export class SimpleRedirect extends Redirect { export class SimpleRedirect extends Redirect {
public redirect(): string { public redirect(): URL {
return this.parameters.redirectValue; return new URL(this.parameters.redirectValue);
} }
} }

View File

@ -1,10 +0,0 @@
import browser from 'webextension-polyfill';
import {updateBadge} from './badge.js';
export async function toggleAllRedirects() {
const state = await browser.storage.local.get({redirectsEnabled: true});
const redirectsEnabled = !(state.redirectsEnabled as boolean);
await browser.storage.local.set({redirectsEnabled});
await updateBadge(redirectsEnabled);
}

View File

@ -84,7 +84,7 @@ test('Redirect.redirect', (t) => {
t.snapshot( t.snapshot(
{ {
original: url instanceof URL ? url.href : url, original: url instanceof URL ? url.href : url,
redirected: new URL(redirect.redirect(url)).href, redirected: redirect.redirect(url).href,
}, },
`${index} ${redirect.constructor.name}`, `${index} ${redirect.constructor.name}`,
); );