Don't redirect if the current tab is already a redirectable URL.

This commit is contained in:
Bauke 2022-11-15 12:24:44 +01:00
parent 4f9b7b81e0
commit e5bf102aa3
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 14 additions and 1 deletions

View File

@ -29,6 +29,10 @@ browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
return;
}
const tab = await browser.tabs.query({active: true, lastFocusedWindow: true});
const currentTabUrl =
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) {
@ -41,6 +45,15 @@ browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
}
if (redirect.isMatch(url)) {
// Don't redirect if the URL before going to a new page is also a match.
// This will happen when the user is already on a website that has a
// redirect, but for whatever reason hasn't redirected. So it's safe to
// assume that they want to stay on this website, rather than get
// redirected.
if (currentTabUrl !== undefined && redirect.isMatch(currentTabUrl)) {
break;
}
const redirectedUrl = redirect.redirect(url);
await browser.tabs.update(details.tabId, {url: redirectedUrl.href});
await browser.storage.local.set({

View File

@ -7,7 +7,7 @@ export default function createManifest(
name: 'Re-Nav',
description: 'Navigation redirects for the masses.',
version: '0.1.1',
permissions: ['storage', 'webNavigation'],
permissions: ['storage', 'tabs', 'webNavigation'],
options_ui: {
page: 'options/index.html',
open_in_tab: true,