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", "@holllo/preact-components": "^0.2.3",
"htm": "^3.1.1", "htm": "^3.1.1",
"modern-normalize": "^1.1.0", "modern-normalize": "^1.1.0",
"nanoid": "^4.0.0",
"preact": "^10.11.0", "preact": "^10.11.0",
"webextension-polyfill": "^0.10.0" "webextension-polyfill": "^0.10.0"
}, },

View File

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

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ import test from 'ava';
import { import {
matcherTypes, matcherTypes,
narrowMatcherType, narrowMatchType,
narrowRedirectType, narrowRedirectType,
parseRedirect, parseRedirect,
redirectTypes, redirectTypes,
@ -40,13 +40,11 @@ test('parseRedirect', (t) => {
]; ];
for (const sample of samples) { for (const sample of samples) {
const redirect = parseRedirect(sample, Redirect.generateId()); const redirect = parseRedirect(sample);
if (redirect === undefined) { if (redirect === undefined) {
t.pass('parseRedirect returned undefined'); t.pass('parseRedirect returned undefined');
} else { } else {
t.regex(redirect.id, /^[a-z]{20}$/i);
redirect.id = 'id';
t.snapshot(redirect, `Class ${redirect.constructor.name}`); t.snapshot(redirect, `Class ${redirect.constructor.name}`);
} }
} }
@ -101,22 +99,8 @@ test('Redirect.isMatch', (t) => {
}); });
test('Narrow match & redirect types', (t) => { test('Narrow match & redirect types', (t) => {
t.false(narrowMatcherType('invalid')); t.false(narrowMatchType('invalid'));
t.false(narrowRedirectType('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))); 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 > Class HostnameRedirect
HostnameRedirect { HostnameRedirect {
id: 'id',
parameters: { parameters: {
hostname: 'example.org', hostname: 'example.org',
matcherType: 'hostname', matcherType: 'hostname',
@ -21,7 +20,6 @@ Generated by [AVA](https://avajs.dev).
> Class SimpleRedirect > Class SimpleRedirect
SimpleRedirect { SimpleRedirect {
id: 'id',
parameters: { parameters: {
matcherType: 'hostname', matcherType: 'hostname',
redirectType: 'simple', redirectType: 'simple',