Fixing and improving redirect core

This commit is contained in:
ManeraKai 2024-08-25 21:31:24 +03:00
parent 14c46a3430
commit dfa6c8e570
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
5 changed files with 72 additions and 70 deletions

View File

@ -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
}

View File

@ -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))
}
})
}
})

View File

@ -18,7 +18,7 @@
<body>
<div id="body">
<h1>You have no instance selected for this frontend</h1>
<h1>LibRedirect: You have no instance selected for this frontend</h1>
</div>
</body>
</html>

View File

@ -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 @@
<Row
class="interactive"
on:click={() => {
browser.runtime.sendMessage("redirectTab", () => {
browser.tabs.update({ url: redirect }, () => {
window.close()
})
}}
@ -58,7 +58,7 @@
<Row
class="interactive"
on:click={async () =>
browser.tabs.update({ url: await servicesHelper.switchInstance(url) }, () => {
browser.tabs.update({ url: switchInstance }, () => {
window.close()
})}
>
@ -75,7 +75,7 @@
<Row
class="interactive"
on:click={() =>
browser.runtime.sendMessage("reverseTab", () => {
browser.tabs.update({ url: redirectToOriginal }, () => {
window.close()
})}
>

View File

@ -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()
})}
>