Compare commits

...

4 Commits

Author SHA1 Message Date
Bauke bb806cb561
Improve manual bypass check.
This makes it so http and https, and www and non-www links are the same.
2022-11-21 14:50:55 +01:00
Bauke 48a29eea71
Add a keyboard shortcut to toggle all redirects, default Alt+Shift+R. 2022-11-21 13:25:00 +01:00
Bauke 6bd5bccd6f
Factor toggling all redirects out to its own file. 2022-11-21 13:23:27 +01:00
Bauke 2acb8d804b
Automatically prefix 'https://' to redirected URLs if they fail parsing. 2022-11-21 12:43:56 +01:00
10 changed files with 74 additions and 15 deletions

View File

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

View File

@ -2,6 +2,7 @@ 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,
@ -48,8 +49,32 @@ 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');
if (redirectDelta < 30_000 && url.href === latestUrl) { // 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) {
return; return;
} }
@ -68,7 +93,16 @@ browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
break; break;
} }
const redirectedUrl = redirect.redirect(url); let 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(),
@ -88,6 +122,8 @@ 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,6 +12,15 @@ 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; public abstract redirect(url: URL | string): URL | string;
} }

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 | SimpleRedirect; export type Redirects = HostnameRedirect | RegexRedirect | 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): URL { public redirect(redirect: URL | string): string {
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 new URL(url.replace(regex, this.parameters.redirectValue)); return 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(): URL { public redirect(): string {
return new URL(this.parameters.redirectValue); return this.parameters.redirectValue;
} }
} }

View File

@ -0,0 +1,10 @@
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: redirect.redirect(url).href, redirected: new URL(redirect.redirect(url)).href,
}, },
`${index} ${redirect.constructor.name}`, `${index} ${redirect.constructor.name}`,
); );