Compare commits

...

3 Commits

Author SHA1 Message Date
Bauke 1fbe1efb90
Don't check redirects in subframes. 2022-11-02 12:16:46 +01:00
Bauke 089a27ab9d
Add regex redirect usage information and an example. 2022-11-02 11:30:02 +01:00
Bauke 2665ee3298
Add a regex redirect. 2022-11-02 11:10:29 +01:00
9 changed files with 86 additions and 5 deletions

View File

@ -19,7 +19,7 @@ browser.runtime.onInstalled.addListener(async () => {
});
browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
if (!details.url.startsWith('http')) {
if (!details.url.startsWith('http') || details.frameId > 0) {
return;
}

View File

@ -95,7 +95,7 @@ export default class Usage extends Component {
<td>https://<b>tildes.net</b>/~creative.timasomo</td>
</tr>
<tr class="alt">
<td class="bold center-text" rowspan="2">Regex</td>
<td class="bold center-text" rowspan="3">Regex</td>
<td class="center-text">HOL{3}O</td>
<td>https://git.bauke.xyz/<b>holllo</b><sup>2</sup></td>
</tr>
@ -103,6 +103,12 @@ export default class Usage extends Component {
<td class="center-text">^https?://www\\.holllo\\.org/?$</td>
<td><b>https://www.holllo.org/</b></td>
</tr>
<tr>
<td class="center-text">
${'^(?<base>https://holllo\\.org)/(?<one>1)-(?<two>2)$'}
</td>
<td><b>https://holllo.org/1-2</b></td>
</tr>
</tbody>
</table>
@ -149,11 +155,21 @@ export default class Usage extends Component {
<td>https://holllo.org/home</td>
<td><b>https://holllo.org</b></td>
</tr>
<tr>
<td class="bold center-text">Regex<sup>2</sup></td>
<td>${'$<base>/$<two>-$<one>'}</td>
<td>https://holllo.org/1-2</td>
<td><b>https://holllo.org/2-1</b></td>
</tr>
</tbody>
</table>
<ol class="footnotes">
<li>The bold highlighted text shows what will be changed.</li>
<li>
The regex redirect only works in combination with the regex matcher,
as the regex matcher will be used for the capturing groups.
</li>
</ol>
</details>
`;

View File

@ -28,6 +28,14 @@ const examples: RedirectParameters[] = [
redirectType: 'simple',
redirectValue: 'https://holllo.org/re-nav',
},
{
enabled: true,
id: -1,
matcherType: 'regex',
matcherValue: '^(?<base>https://holllo\\.org)/(?<one>1)-(?<two>2)$',
redirectType: 'regex',
redirectValue: '$<base>/$<two>-$<one>',
},
];
export async function generateExamples(): Promise<

View File

@ -1,5 +1,5 @@
export const matcherTypes = ['hostname', 'regex'] as const;
export const redirectTypes = ['hostname', 'simple'] as const;
export const redirectTypes = ['hostname', 'regex', 'simple'] as const;
export type MatcherType = typeof matcherTypes[number];
export type RedirectType = typeof redirectTypes[number];

View File

@ -1,9 +1,11 @@
import {RedirectParameters} from './base.js';
import {HostnameRedirect} from './hostname.js';
import {RegexRedirect} from './regex.js';
import {SimpleRedirect} from './simple.js';
export * from './base.js';
export * from './hostname.js';
export * from './regex.js';
export * from './simple.js';
export type Redirects = HostnameRedirect | SimpleRedirect;
@ -11,6 +13,7 @@ export type Redirects = HostnameRedirect | SimpleRedirect;
export function parseRedirect(
parameters: RedirectParameters,
): Redirects | undefined {
const matcherType = parameters?.matcherType;
const redirectType = parameters?.redirectType;
if (redirectType === 'hostname') {
@ -20,4 +23,8 @@ export function parseRedirect(
if (redirectType === 'simple') {
return new SimpleRedirect(parameters);
}
if (matcherType === 'regex' && redirectType === 'regex') {
return new RegexRedirect(parameters);
}
}

9
source/redirect/regex.ts Normal file
View File

@ -0,0 +1,9 @@
import {Redirect} from './base.js';
export class RegexRedirect extends Redirect {
public redirect(redirect: URL | string): URL {
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));
}
}

View File

@ -12,6 +12,7 @@ import {
Redirect,
Redirects,
RedirectParameters,
RegexRedirect,
SimpleRedirect,
} from '../source/redirect/exports.js';
@ -33,6 +34,15 @@ const simpleParameters: RedirectParameters = {
redirectValue: 'https://example.org/simple',
};
const regexParameters: RedirectParameters = {
enabled: true,
id: 3,
matcherType: 'regex',
matcherValue: '(.+)\\.com',
redirectType: 'regex',
redirectValue: '$1.org',
};
test('parseRedirect', (t) => {
const samples: RedirectParameters[] = [
{
@ -41,6 +51,7 @@ test('parseRedirect', (t) => {
undefined as unknown as RedirectParameters,
hostnameParameters,
simpleParameters,
regexParameters,
];
for (const sample of samples) {
@ -58,18 +69,21 @@ test('parseRedirect', (t) => {
test('Redirect.redirect', (t) => {
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
const simpleRedirect = new SimpleRedirect(simpleParameters);
const regexRedirect = new RegexRedirect(regexParameters);
const samples: Array<[string, Redirect]> = [
const samples: Array<[string | URL, Redirect]> = [
['https://example.com', hostnameRedirect],
['https://example.com/path#hash?query=test', hostnameRedirect],
['https://example.com', simpleRedirect],
['https://example.com/path', simpleRedirect],
['https://example.com', regexRedirect],
[new URL('https://example.com'), regexRedirect],
];
for (const [index, [url, redirect]] of samples.entries()) {
t.snapshot(
{
original: url,
original: url instanceof URL ? url.href : url,
redirected: redirect.redirect(url).href,
},
`${index} ${redirect.constructor.name}`,

View File

@ -32,6 +32,19 @@ Generated by [AVA](https://avajs.dev).
},
}
> Class RegexRedirect
RegexRedirect {
parameters: {
enabled: true,
id: 3,
matcherType: 'regex',
matcherValue: '(.+)\\.com',
redirectType: 'regex',
redirectValue: '$1.org',
},
}
## Redirect.redirect
> 0 HostnameRedirect
@ -61,3 +74,17 @@ Generated by [AVA](https://avajs.dev).
original: 'https://example.com/path',
redirected: 'https://example.org/simple',
}
> 4 RegexRedirect
{
original: 'https://example.com',
redirected: 'https://example.org/',
}
> 5 RegexRedirect
{
original: 'https://example.com/',
redirected: 'https://example.org/',
}