Fixing and improving redirect core
This commit is contained in:
parent
14c46a3430
commit
dfa6c8e570
|
@ -55,9 +55,9 @@ function regexArray(service, url, config, options, frontend) {
|
||||||
* @param {URL} originUrl
|
* @param {URL} originUrl
|
||||||
* @param {boolean} forceRedirection
|
* @param {boolean} forceRedirection
|
||||||
*/
|
*/
|
||||||
async function redirectAsync(url, type, originUrl, documentUrl, forceRedirection) {
|
async function redirectAsync(url, type, originUrl, documentUrl, incognito, forceRedirection) {
|
||||||
await init()
|
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 {URL} url
|
||||||
* @param {string} type
|
* @param {string} type
|
||||||
* @param {URL} originUrl
|
* @param {URL} originUrl
|
||||||
|
* @param {URL} documentUrl
|
||||||
|
* @param {boolean} incognito
|
||||||
* @param {boolean} forceRedirection
|
* @param {boolean} forceRedirection
|
||||||
* @returns {string | undefined}
|
* @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
|
if (type != "main_frame" && type != "sub_frame" && type != "image") return
|
||||||
let randomInstance
|
let randomInstance
|
||||||
let frontend
|
let frontend
|
||||||
if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return
|
if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return
|
||||||
for (const service in config.services) {
|
for (const service in config.services) {
|
||||||
if (!forceRedirection && !options[service].enabled) continue
|
if (!forceRedirection && !options[service].enabled) continue
|
||||||
|
if (!forceRedirection && options[service].redirectOnlyInIncognito == true && !incognito) continue
|
||||||
|
|
||||||
frontend = options[service].frontend
|
frontend = options[service].frontend
|
||||||
|
|
||||||
if (options[service].redirectOnlyInIncognito == true && !incognito) continue
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
config.services[service].frontends[frontend].desktopApp &&
|
config.services[service].frontends[frontend].desktopApp &&
|
||||||
type != "main_frame" &&
|
type != "main_frame" &&
|
||||||
|
@ -613,11 +614,11 @@ function redirect(url, type, originUrl, documentUrl, forceRedirection, incognito
|
||||||
|
|
||||||
let instanceList = options[frontend]
|
let instanceList = options[frontend]
|
||||||
if (instanceList === undefined) break
|
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 (originUrl && instanceList.includes(originUrl.origin)) {
|
||||||
if (type != "main_frame") return null
|
if (type == "main_frame") return "BYPASSTAB"
|
||||||
else return "BYPASSTAB"
|
else return null
|
||||||
}
|
}
|
||||||
|
|
||||||
randomInstance = utils.getRandomInstance(instanceList)
|
randomInstance = utils.getRandomInstance(instanceList)
|
||||||
|
@ -937,10 +938,18 @@ function isException(url) {
|
||||||
for (let item of exceptions.url) {
|
for (let item of exceptions.url) {
|
||||||
item = new URL(item)
|
item = new URL(item)
|
||||||
item = item.href.replace(/^http:\/\//, "https://")
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,58 +19,71 @@ browser.runtime.onInstalled.addListener(async details => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// true to redirect, false to bypass
|
||||||
let tabIdRedirects = {}
|
let tabIdRedirects = {}
|
||||||
|
|
||||||
// true == Always redirect, false == Never redirect, null/undefined == follow options for services
|
// true == Always redirect, false == Never redirect, null/undefined == follow options for services
|
||||||
browser.webRequest.onBeforeRequest.addListener(
|
browser.webRequest.onBeforeRequest.addListener(
|
||||||
details => {
|
details => {
|
||||||
|
const old_href = details.url
|
||||||
const url = new URL(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")
|
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 originUrl
|
||||||
|
let documentUrl
|
||||||
try {
|
try {
|
||||||
if (details.originUrl) originUrl = new URL(details.originUrl)
|
if (details.originUrl) originUrl = new URL(details.originUrl)
|
||||||
|
if (details.documentUrl) documentUrl = new URL(details.documentUrl)
|
||||||
} catch {
|
} catch {
|
||||||
return null
|
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 (
|
const newUrl = servicesHelper.redirect(
|
||||||
details.frameAncestors &&
|
url,
|
||||||
details.frameAncestors.length > 0 &&
|
details.type,
|
||||||
servicesHelper.isException(new URL(details.frameAncestors[0].url))
|
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) {
|
if (!newUrl) {
|
||||||
const match = url.href.match(/^https?:\/{2}.*\.libredirect\.invalid.*/)
|
const match = url.href.match(/^https?:\/{2}(.*\.)?libredirect\.invalid.*/)
|
||||||
if (match) {
|
if (match) {
|
||||||
browser.tabs.update({
|
browser.tabs.update({ url: browser.runtime.getURL(`/pages/messages/no_instance.html`) })
|
||||||
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) {
|
||||||
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)
|
console.log("Redirecting", old_href, "=>", newUrl)
|
||||||
return { redirectUrl: newUrl }
|
return { redirectUrl: newUrl }
|
||||||
}
|
}
|
||||||
|
@ -105,7 +118,7 @@ browser.runtime.getPlatformInfo(r => {
|
||||||
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
|
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
|
||||||
if (tabs[0].url) {
|
if (tabs[0].url) {
|
||||||
const url = new URL(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) {
|
if (newUrl) {
|
||||||
browser.tabs.update(tabs[0].id, { url: newUrl }, () => {
|
browser.tabs.update(tabs[0].id, { url: newUrl }, () => {
|
||||||
tabIdRedirects[tabs[0].id] = true
|
tabIdRedirects[tabs[0].id] = true
|
||||||
|
@ -211,7 +224,7 @@ browser.runtime.getPlatformInfo(r => {
|
||||||
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
|
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
|
||||||
if (tabs[0].url) {
|
if (tabs[0].url) {
|
||||||
const url = new URL(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) {
|
if (newUrl) {
|
||||||
browser.tabs.update(tabs[0].id, { url: newUrl }, () => {
|
browser.tabs.update(tabs[0].id, { url: newUrl }, () => {
|
||||||
tabIdRedirects[tabs[0].id] = true
|
tabIdRedirects[tabs[0].id] = true
|
||||||
|
@ -228,7 +241,7 @@ browser.runtime.getPlatformInfo(r => {
|
||||||
case "redirectLink":
|
case "redirectLink":
|
||||||
case "redirectLinkInNewTab": {
|
case "redirectLinkInNewTab": {
|
||||||
const url = new URL(info.linkUrl)
|
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 (newUrl) {
|
||||||
if (info.menuItemId == "redirectLink") browser.tabs.update({ url: newUrl })
|
if (info.menuItemId == "redirectLink") browser.tabs.update({ url: newUrl })
|
||||||
else browser.tabs.create({ url: newUrl })
|
else browser.tabs.create({ url: newUrl })
|
||||||
|
@ -276,7 +289,7 @@ browser.runtime.getPlatformInfo(r => {
|
||||||
case "redirectBookmarkInNewTab":
|
case "redirectBookmarkInNewTab":
|
||||||
browser.bookmarks.get(info.bookmarkId, bookmarks => {
|
browser.bookmarks.get(info.bookmarkId, bookmarks => {
|
||||||
const url = new URL(bookmarks[0].url)
|
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 (newUrl) {
|
||||||
if (info.menuItemId == "redirectBookmark") browser.tabs.update({ url: newUrl })
|
if (info.menuItemId == "redirectBookmark") browser.tabs.update({ url: newUrl })
|
||||||
else browser.tabs.create({ 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))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div id="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>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
url = new URL(tabs[0].url)
|
url = new URL(tabs[0].url)
|
||||||
servicesHelper.switchInstance(url).then(r => (switchInstance = r))
|
servicesHelper.switchInstance(url).then(r => (switchInstance = r))
|
||||||
servicesHelper.reverse(url).then(r => (redirectToOriginal = 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))
|
servicesHelper.computeService(url).then(r => (currentService = r))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -44,7 +44,7 @@
|
||||||
<Row
|
<Row
|
||||||
class="interactive"
|
class="interactive"
|
||||||
on:click={() => {
|
on:click={() => {
|
||||||
browser.runtime.sendMessage("redirectTab", () => {
|
browser.tabs.update({ url: redirect }, () => {
|
||||||
window.close()
|
window.close()
|
||||||
})
|
})
|
||||||
}}
|
}}
|
||||||
|
@ -58,7 +58,7 @@
|
||||||
<Row
|
<Row
|
||||||
class="interactive"
|
class="interactive"
|
||||||
on:click={async () =>
|
on:click={async () =>
|
||||||
browser.tabs.update({ url: await servicesHelper.switchInstance(url) }, () => {
|
browser.tabs.update({ url: switchInstance }, () => {
|
||||||
window.close()
|
window.close()
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
|
@ -75,7 +75,7 @@
|
||||||
<Row
|
<Row
|
||||||
class="interactive"
|
class="interactive"
|
||||||
on:click={() =>
|
on:click={() =>
|
||||||
browser.runtime.sendMessage("reverseTab", () => {
|
browser.tabs.update({ url: redirectToOriginal }, () => {
|
||||||
window.close()
|
window.close()
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
class="interactive margin margin_{document.body.dir}"
|
class="interactive margin margin_{document.body.dir}"
|
||||||
on:keydown={null}
|
on:keydown={null}
|
||||||
on:click={() =>
|
on:click={() =>
|
||||||
browser.tabs.create({ url: browser.runtime.getURL(_config.services[serviceKey].url) }, () => {
|
browser.tabs.create({ url: _config.services[serviceKey].url }, () => {
|
||||||
window.close()
|
window.close()
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
|
|
Loading…
Reference in New Issue