Compare commits
4 Commits
6ab041df93
...
bb806cb561
Author | SHA1 | Date |
---|---|---|
Bauke | bb806cb561 | |
Bauke | 48a29eea71 | |
Bauke | 6bd5bccd6f | |
Bauke | 2acb8d804b |
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
import browser from 'webextension-polyfill';
|
||||
|
||||
import {updateBadge} from '../utilities/badge.js';
|
||||
import {toggleAllRedirects} from '../utilities/toggle-all-redirects.js';
|
||||
|
||||
type ContextMenu = browser.Menus.CreateCreatePropertiesType;
|
||||
|
||||
|
@ -48,9 +48,6 @@ export async function contextClicked(
|
|||
}
|
||||
|
||||
if (id === 're-nav-toggle-redirects') {
|
||||
const state = await browser.storage.local.get({redirectsEnabled: true});
|
||||
const redirectsEnabled = !(state.redirectsEnabled as boolean);
|
||||
await browser.storage.local.set({redirectsEnabled});
|
||||
await updateBadge(redirectsEnabled);
|
||||
await toggleAllRedirects();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import browser from 'webextension-polyfill';
|
|||
|
||||
import storage from '../redirect/storage.js';
|
||||
import {updateBadge} from '../utilities/badge.js';
|
||||
import {onCommandsHandler} from './commands.js';
|
||||
import {
|
||||
contextClicked,
|
||||
getContextMenus,
|
||||
|
@ -48,8 +49,32 @@ browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
|
|||
tab[0]?.url === undefined ? undefined : new URL(tab[0].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;
|
||||
}
|
||||
|
||||
|
@ -68,7 +93,16 @@ browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
|
|||
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.storage.local.set({
|
||||
latestTime: Date.now(),
|
||||
|
@ -88,6 +122,8 @@ browser.contextMenus.onClicked.addListener(async (info, tab) => {
|
|||
await contextClicked(contextMenuIds, info, tab);
|
||||
});
|
||||
|
||||
browser.commands.onCommand.addListener(onCommandsHandler);
|
||||
|
||||
if (import.meta.env.VITE_BROWSER === 'chromium') {
|
||||
browser.action.onClicked.addListener(browserActionClicked);
|
||||
} else {
|
||||
|
|
|
@ -12,6 +12,15 @@ export default function createManifest(
|
|||
page: 'options/index.html',
|
||||
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 = {
|
||||
|
|
|
@ -48,5 +48,5 @@ export abstract class Redirect {
|
|||
return false;
|
||||
}
|
||||
|
||||
public abstract redirect(url: URL | string): URL;
|
||||
public abstract redirect(url: URL | string): URL | string;
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ export * from './hostname.js';
|
|||
export * from './regex.js';
|
||||
export * from './simple.js';
|
||||
|
||||
export type Redirects = HostnameRedirect | SimpleRedirect;
|
||||
export type Redirects = HostnameRedirect | RegexRedirect | SimpleRedirect;
|
||||
|
||||
export function parseRedirect(
|
||||
parameters: RedirectParameters,
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import {Redirect} from './base.js';
|
||||
|
||||
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 regex = new RegExp(this.parameters.matcherValue, 'gi');
|
||||
return new URL(url.replace(regex, this.parameters.redirectValue));
|
||||
return url.replace(regex, this.parameters.redirectValue);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {Redirect} from './base.js';
|
||||
|
||||
export class SimpleRedirect extends Redirect {
|
||||
public redirect(): URL {
|
||||
return new URL(this.parameters.redirectValue);
|
||||
public redirect(): string {
|
||||
return this.parameters.redirectValue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -84,7 +84,7 @@ test('Redirect.redirect', (t) => {
|
|||
t.snapshot(
|
||||
{
|
||||
original: url instanceof URL ? url.href : url,
|
||||
redirected: redirect.redirect(url).href,
|
||||
redirected: new URL(redirect.redirect(url)).href,
|
||||
},
|
||||
`${index} ${redirect.constructor.name}`,
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue