Compare commits

..

No commits in common. "d25a844facff9d9973c49e14db542b769b3ca1dd" and "89751faad2cc7c19b5a111e512772b608968dc1f" have entirely different histories.

8 changed files with 11 additions and 57 deletions

View File

@ -20,7 +20,6 @@
"@holllo/preact-components": "^0.2.3",
"htm": "^3.1.1",
"modern-normalize": "^1.1.0",
"nanoid": "^4.0.0",
"preact": "^10.11.0",
"webextension-polyfill": "^0.10.0"
},

View File

@ -10,7 +10,6 @@ specifiers:
c8: ^7.12.0
htm: ^3.1.1
modern-normalize: ^1.1.0
nanoid: ^4.0.0
postcss: ^8.4.16
preact: ^10.11.0
sass: ^1.55.0
@ -30,7 +29,6 @@ dependencies:
'@holllo/preact-components': 0.2.3_htm@3.1.1+preact@10.11.0
htm: 3.1.1
modern-normalize: 1.1.0
nanoid: 4.0.0
preact: 10.11.0
webextension-polyfill: 0.10.0
@ -5199,12 +5197,6 @@ packages:
hasBin: true
dev: true
/nanoid/4.0.0:
resolution: {integrity: sha512-IgBP8piMxe/gf73RTQx7hmnhwz0aaEXYakvqZyE302IXW3HyVNhdNGC+O2MwMAVhLEnvXlvKtGbtJf6wvHihCg==}
engines: {node: ^14 || ^16 || >=18}
hasBin: true
dev: false
/natural-compare/1.4.0:
resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==}
dev: true
@ -7645,11 +7637,6 @@ packages:
resolution: {integrity: sha512-liCEteZ5z+QRyh3XzsYWQyxedBHBvx8CDlNvvi+BJz74L0E5/ID2v7JtoX3bD541AlMuOy4e/iWif6hhNGBFNw==}
engines: {node: '>=12.20'}
hasBin: true
peerDependencies:
webpack: '>=1.11.0'
peerDependenciesMeta:
webpack:
optional: true
dependencies:
'@eslint/eslintrc': 1.3.2
'@typescript-eslint/eslint-plugin': 5.36.2_q7mrctxvxoqtj4jpjqmbsyg2qy
@ -7690,6 +7677,7 @@ packages:
transitivePeerDependencies:
- eslint-import-resolver-typescript
- supports-color
- webpack
dev: true
bundledDependencies:
- '@typescript-eslint/eslint-plugin'

View File

@ -21,10 +21,8 @@ browser.runtime.onInstalled.addListener(async () => {
browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
const url = new URL(details.url);
for (const [id, parameters] of Object.entries(
await browser.storage.local.get(),
)) {
const redirect = parseRedirect(parameters, id);
for (const parameters of Object.values(await browser.storage.local.get())) {
const redirect = parseRedirect(parameters);
if (redirect === undefined) {
continue;
}

View File

@ -1,12 +1,10 @@
import {customAlphabet} from 'nanoid';
export const matcherTypes = ['hostname'] as const;
export const redirectTypes = ['hostname', 'simple'] as const;
export type MatcherType = typeof matcherTypes[number];
export type RedirectType = typeof redirectTypes[number];
export function narrowMatcherType(value: string): value is MatcherType {
export function narrowMatchType(value: string): value is MatcherType {
return matcherTypes.includes(value as MatcherType);
}
@ -24,17 +22,7 @@ export type RedirectParameters = {
};
export abstract class Redirect<P extends RedirectParameters> {
public static generateId(): string {
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const nanoid = customAlphabet(`${alphabet}${alphabet.toUpperCase()}`, 20);
return nanoid();
}
public id: string;
constructor(public parameters: P & Matcher, id?: string) {
this.id = id ?? Redirect.generateId();
}
constructor(public parameters: P & Matcher) {}
public isMatch(url: URL): boolean {
if (this.parameters.matcherType === 'hostname') {

View File

@ -9,15 +9,14 @@ export type Redirects = HostnameRedirect | SimpleRedirect;
export function parseRedirect<P extends Redirects['parameters']>(
parameters: P,
id: string,
): Redirects | undefined {
const redirectType = parameters?.redirectType;
if (redirectType === 'hostname') {
return new HostnameRedirect(parameters, id);
return new HostnameRedirect(parameters);
}
if (redirectType === 'simple') {
return new SimpleRedirect(parameters, id);
return new SimpleRedirect(parameters);
}
}

View File

@ -4,7 +4,7 @@ import test from 'ava';
import {
matcherTypes,
narrowMatcherType,
narrowMatchType,
narrowRedirectType,
parseRedirect,
redirectTypes,
@ -40,13 +40,11 @@ test('parseRedirect', (t) => {
];
for (const sample of samples) {
const redirect = parseRedirect(sample, Redirect.generateId());
const redirect = parseRedirect(sample);
if (redirect === undefined) {
t.pass('parseRedirect returned undefined');
} else {
t.regex(redirect.id, /^[a-z]{20}$/i);
redirect.id = 'id';
t.snapshot(redirect, `Class ${redirect.constructor.name}`);
}
}
@ -101,22 +99,8 @@ test('Redirect.isMatch', (t) => {
});
test('Narrow match & redirect types', (t) => {
t.false(narrowMatcherType('invalid'));
t.false(narrowMatchType('invalid'));
t.false(narrowRedirectType('invalid'));
t.true(matcherTypes.every((value) => narrowMatcherType(value)));
t.true(matcherTypes.every((value) => narrowMatchType(value)));
t.true(redirectTypes.every((value) => narrowRedirectType(value)));
});
test('Redirect getters & setters', (t) => {
const samples: Array<[Redirects, string]> = [
[new HostnameRedirect(hostnameParameters), hostnameParameters.hostname],
[new SimpleRedirect(simpleParameters), simpleParameters.target],
];
for (const [redirect, value] of samples) {
t.is(redirect.redirectValue, value);
const newValue = `${value} test`;
redirect.redirectValue = newValue;
t.is(redirect.redirectValue, newValue);
}
});

View File

@ -9,7 +9,6 @@ Generated by [AVA](https://avajs.dev).
> Class HostnameRedirect
HostnameRedirect {
id: 'id',
parameters: {
hostname: 'example.org',
matcherType: 'hostname',
@ -21,7 +20,6 @@ Generated by [AVA](https://avajs.dev).
> Class SimpleRedirect
SimpleRedirect {
id: 'id',
parameters: {
matcherType: 'hostname',
redirectType: 'simple',