From dfa6c8e570e3030c382ad045156a0b12fd3ba368 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sun, 25 Aug 2024 21:31:24 +0300 Subject: [PATCH] Fixing and improving redirect core --- src/assets/javascripts/services.js | 29 ++++-- src/pages/background/background.js | 101 +++++++++---------- src/pages/messages/no_instance.html | 2 +- src/pages/popup_src/Buttons.svelte | 8 +- src/pages/popup_src/components/Switch.svelte | 2 +- 5 files changed, 72 insertions(+), 70 deletions(-) diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index d3afd43..e93b637 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -55,9 +55,9 @@ function regexArray(service, url, config, options, frontend) { * @param {URL} originUrl * @param {boolean} forceRedirection */ -async function redirectAsync(url, type, originUrl, documentUrl, forceRedirection) { +async function redirectAsync(url, type, originUrl, documentUrl, incognito, forceRedirection) { await init() - return redirect(url, type, originUrl, documentUrl, forceRedirection) + return redirect(url, type, originUrl, documentUrl, incognito, forceRedirection) } /** @@ -569,21 +569,22 @@ function rewrite(url, originUrl, frontend, randomInstance) { * @param {URL} url * @param {string} type * @param {URL} originUrl + * @param {URL} documentUrl + * @param {boolean} incognito * @param {boolean} forceRedirection * @returns {string | undefined} */ -function redirect(url, type, originUrl, documentUrl, forceRedirection, incognito) { +function redirect(url, type, originUrl, documentUrl, incognito, forceRedirection) { if (type != "main_frame" && type != "sub_frame" && type != "image") return let randomInstance let frontend if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return for (const service in config.services) { if (!forceRedirection && !options[service].enabled) continue + if (!forceRedirection && options[service].redirectOnlyInIncognito == true && !incognito) continue frontend = options[service].frontend - if (options[service].redirectOnlyInIncognito == true && !incognito) continue - if ( config.services[service].frontends[frontend].desktopApp && type != "main_frame" && @@ -613,11 +614,11 @@ function redirect(url, type, originUrl, documentUrl, forceRedirection, incognito let instanceList = options[frontend] if (instanceList === undefined) break - if (instanceList.length === 0) return null + if (instanceList.length === 0) return "https://libredirect.invalid" if (originUrl && instanceList.includes(originUrl.origin)) { - if (type != "main_frame") return null - else return "BYPASSTAB" + if (type == "main_frame") return "BYPASSTAB" + else return null } randomInstance = utils.getRandomInstance(instanceList) @@ -937,10 +938,18 @@ function isException(url) { for (let item of exceptions.url) { item = new URL(item) item = item.href.replace(/^http:\/\//, "https://") - if (item == url.href) return true + if (item == url.href) { + return true + } + } + } + if (exceptions.regex) { + for (const item of exceptions.regex) { + if (new RegExp(item).test(url.href)) { + return true + } } } - if (exceptions.regex) for (const item of exceptions.regex) if (new RegExp(item).test(url.href)) return true } return false } diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 16ca38d..f41cb55 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -19,58 +19,71 @@ browser.runtime.onInstalled.addListener(async details => { } }) +// true to redirect, false to bypass let tabIdRedirects = {} // true == Always redirect, false == Never redirect, null/undefined == follow options for services browser.webRequest.onBeforeRequest.addListener( details => { + const old_href = details.url const url = new URL(details.url) - const old_href = url.href if (new RegExp(/^chrome-extension:\/{2}.*\/instances\/.*.json$/).test(url.href) && details.type == "xmlhttprequest") - return + return null + + // if url is previously bypassed + if (tabIdRedirects[details.tabId] == false) return null + + // Bypass embeds from excepted urls + if ( + details.frameAncestors && + details.frameAncestors.length >= 1 && + servicesHelper.isException(new URL(details.frameAncestors[0].url)) + ) + return null + + if (servicesHelper.isException(url)) { + if (details.type == "main_frame") { + console.log(`Bypassing ${details.tabId} ${url}`) + tabIdRedirects[details.tabId] = false + } + return null + } + let originUrl + let documentUrl try { if (details.originUrl) originUrl = new URL(details.originUrl) + if (details.documentUrl) documentUrl = new URL(details.documentUrl) } catch { return null } - let documentUrl - try { if (details.documentUrl) documentUrl = new URL(details.documentUrl) } - catch (error) { return null } - if (tabIdRedirects[details.tabId] == false) return null - let newUrl = servicesHelper.redirect(url, details.type, originUrl, documentUrl, tabIdRedirects[details.tabId], details.incognito) - if ( - details.frameAncestors && - details.frameAncestors.length > 0 && - servicesHelper.isException(new URL(details.frameAncestors[0].url)) + const newUrl = servicesHelper.redirect( + url, + details.type, + originUrl, + documentUrl, + details.incognito, + tabIdRedirects[details.tabId] ) - newUrl = null - - if (servicesHelper.isException(url)) { - if (details.type == "main_frame") newUrl = "BYPASSTAB" - else return null - } if (!newUrl) { - const match = url.href.match(/^https?:\/{2}.*\.libredirect\.invalid.*/) + const match = url.href.match(/^https?:\/{2}(.*\.)?libredirect\.invalid.*/) if (match) { - browser.tabs.update({ - url: browser.runtime.getURL(`/pages/messages/no_instance.html`), - }) + browser.tabs.update({ url: browser.runtime.getURL(`/pages/messages/no_instance.html`) }) } } + if (newUrl === "CANCEL") { + console.log(`Cancelling ${url}`) + return { cancel: true } + } + if (newUrl === "BYPASSTAB") { + console.log(`Bypassing ${details.tabId} ${url}`) + tabIdRedirects[details.tabId] = false + return null + } if (newUrl) { - if (newUrl === "CANCEL") { - console.log(`Canceled ${url}`) - return { cancel: true } - } - if (newUrl === "BYPASSTAB") { - console.log(`Bypassed ${details.tabId} ${url}`) - if (tabIdRedirects[details.tabId] != false) tabIdRedirects[details.tabId] = false - return null - } console.log("Redirecting", old_href, "=>", newUrl) return { redirectUrl: newUrl } } @@ -105,7 +118,7 @@ browser.runtime.getPlatformInfo(r => { browser.tabs.query({ active: true, currentWindow: true }, async tabs => { if (tabs[0].url) { const url = new URL(tabs[0].url) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) + const newUrl = servicesHelper.redirect(url, "main_frame", null, null, false, true) if (newUrl) { browser.tabs.update(tabs[0].id, { url: newUrl }, () => { tabIdRedirects[tabs[0].id] = true @@ -211,7 +224,7 @@ browser.runtime.getPlatformInfo(r => { browser.tabs.query({ active: true, currentWindow: true }, async tabs => { if (tabs[0].url) { const url = new URL(tabs[0].url) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) + const newUrl = servicesHelper.redirect(url, "main_frame", null, null, false, true) if (newUrl) { browser.tabs.update(tabs[0].id, { url: newUrl }, () => { tabIdRedirects[tabs[0].id] = true @@ -228,7 +241,7 @@ browser.runtime.getPlatformInfo(r => { case "redirectLink": case "redirectLinkInNewTab": { const url = new URL(info.linkUrl) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) + const newUrl = servicesHelper.redirect(url, "main_frame", null, null, false, true) if (newUrl) { if (info.menuItemId == "redirectLink") browser.tabs.update({ url: newUrl }) else browser.tabs.create({ url: newUrl }) @@ -276,7 +289,7 @@ browser.runtime.getPlatformInfo(r => { case "redirectBookmarkInNewTab": browser.bookmarks.get(info.bookmarkId, bookmarks => { const url = new URL(bookmarks[0].url) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) + const newUrl = servicesHelper.redirect(url, "main_frame", null, null, false, true) if (newUrl) { if (info.menuItemId == "redirectBookmark") browser.tabs.update({ url: newUrl }) else browser.tabs.create({ url: newUrl }) @@ -316,23 +329,3 @@ browser.runtime.getPlatformInfo(r => { }) } }) - -browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - if (request == "reverseTab") { - browser.tabs.query({ active: true, currentWindow: true }, async tabs => { - if (tabs[0].url) { - const url = new URL(tabs[0].url) - const newUrl = await servicesHelper.reverse(url) - if (newUrl) browser.tabs.update(tabs[0].id, { url: newUrl }, () => (tabIdRedirects[tabs[0].id] = false)) - } - }) - } else if (request == "redirectTab") { - browser.tabs.query({ active: true, currentWindow: true }, async tabs => { - if (tabs[0].url) { - const url = new URL(tabs[0].url) - const newUrl = servicesHelper.redirect(url, "main_frame", null, null, true) - if (newUrl) browser.tabs.update(tabs[0].id, { url: newUrl }, () => (tabIdRedirects[tabs[0].id] = true)) - } - }) - } -}) diff --git a/src/pages/messages/no_instance.html b/src/pages/messages/no_instance.html index 358ff50..55e5fac 100644 --- a/src/pages/messages/no_instance.html +++ b/src/pages/messages/no_instance.html @@ -18,7 +18,7 @@
-

You have no instance selected for this frontend

+

LibRedirect: You have no instance selected for this frontend

diff --git a/src/pages/popup_src/Buttons.svelte b/src/pages/popup_src/Buttons.svelte index e53a64b..c0644ff 100644 --- a/src/pages/popup_src/Buttons.svelte +++ b/src/pages/popup_src/Buttons.svelte @@ -33,7 +33,7 @@ url = new URL(tabs[0].url) servicesHelper.switchInstance(url).then(r => (switchInstance = r)) servicesHelper.reverse(url).then(r => (redirectToOriginal = r)) - servicesHelper.redirectAsync(url, "main_frame", null, true).then(r => (redirect = r)) + servicesHelper.redirectAsync(url, "main_frame", null, null, false, true).then(r => (redirect = r)) servicesHelper.computeService(url).then(r => (currentService = r)) } }) @@ -44,7 +44,7 @@ { - browser.runtime.sendMessage("redirectTab", () => { + browser.tabs.update({ url: redirect }, () => { window.close() }) }} @@ -58,7 +58,7 @@ - browser.tabs.update({ url: await servicesHelper.switchInstance(url) }, () => { + browser.tabs.update({ url: switchInstance }, () => { window.close() })} > @@ -75,7 +75,7 @@ - browser.runtime.sendMessage("reverseTab", () => { + browser.tabs.update({ url: redirectToOriginal }, () => { window.close() })} > diff --git a/src/pages/popup_src/components/Switch.svelte b/src/pages/popup_src/components/Switch.svelte index f76e74b..98f765b 100644 --- a/src/pages/popup_src/components/Switch.svelte +++ b/src/pages/popup_src/components/Switch.svelte @@ -30,7 +30,7 @@ class="interactive margin margin_{document.body.dir}" on:keydown={null} on:click={() => - browser.tabs.create({ url: browser.runtime.getURL(_config.services[serviceKey].url) }, () => { + browser.tabs.create({ url: _config.services[serviceKey].url }, () => { window.close() })} >