Merge branch '1fexd-refactor-redirect'

This commit is contained in:
ManeraKai 2024-03-14 12:49:48 +03:00
commit 498801763f
1 changed files with 62 additions and 56 deletions

View File

@ -62,68 +62,21 @@ async function redirectAsync(url, type, initiator, forceRedirection) {
} }
/** /**
* @param {URL} url * @param url
* @param {string} type * @param frontend
* @param {URL} initiator * @param randomInstance
* @param {boolean} forceRedirection * @returns {undefined|string}
* @returns {string | undefined}
*/ */
function redirect(url, type, initiator, forceRedirection, incognito) { function rewrite(url, frontend, randomInstance) {
if (type != "main_frame" && type != "sub_frame" && type != "image") return if (!frontend || !randomInstance) return
let randomInstance
let frontend
if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return
for (const service in config.services) {
if (!forceRedirection && !options[service].enabled) continue
frontend = options[service].frontend
if (config.services[service].frontends[frontend].desktopApp && type != "main_frame" && options[service].redirectType != "main_frame")
frontend = options[service].embedFrontend
if (!regexArray(service, url, config, frontend)) {
frontend = null
continue
}
if (
config.services[service].embeddable &&
type != options[service].redirectType && options[service].redirectType != "both"
) {
if (options[service].unsupportedUrls == 'block') return 'CANCEL'
return
}
let instanceList = options[frontend]
if (instanceList === undefined) break
if (instanceList.length === 0) return null
if (
initiator
&&
instanceList.includes(initiator.origin)
) {
if (type != "main_frame") return null
else return "BYPASSTAB"
}
randomInstance = utils.getRandomInstance(instanceList)
if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") {
randomInstance = `http://${frontend}.localhost:8080`
}
break
}
if (!frontend) return
switch (frontend) { switch (frontend) {
case "hyperpipe": { case "hyperpipe": {
return `${randomInstance}${url.pathname}${url.search}`.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/")) return `${randomInstance}${url.pathname}${url.search}`.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/"))
} }
case "searx": case "searx":
case "searxng": case "searxng": {
return `${randomInstance}/${url.search}` return `${randomInstance}/${url.search}`
}
case "whoogle": { case "whoogle": {
return `${randomInstance}/search${url.search}` return `${randomInstance}/search${url.search}`
} }
@ -149,7 +102,6 @@ function redirect(url, type, initiator, forceRedirection, incognito) {
case "freetubePwa": { case "freetubePwa": {
return 'freetube://' + url.href return 'freetube://' + url.href
} }
case "poketube": { case "poketube": {
if (url.pathname.startsWith('/channel')) { if (url.pathname.startsWith('/channel')) {
const reg = /\/channel\/(.*)\/?$/.exec(url.pathname) const reg = /\/channel\/(.*)\/?$/.exec(url.pathname)
@ -557,6 +509,60 @@ function redirect(url, type, initiator, forceRedirection, incognito) {
} }
} }
/**
* @param {URL} url
* @param {string} type
* @param {URL} initiator
* @param {boolean} forceRedirection
* @returns {string | undefined}
*/
function redirect(url, type, initiator, forceRedirection, incognito) {
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
frontend = options[service].frontend
if (config.services[service].frontends[frontend].desktopApp && type != "main_frame" && options[service].redirectType != "main_frame")
frontend = options[service].embedFrontend
if (!regexArray(service, url, config, frontend)) {
frontend = null
continue
}
if (
config.services[service].embeddable
&&
type != options[service].redirectType && options[service].redirectType != "both"
) {
if (options[service].unsupportedUrls == 'block') return 'CANCEL'
return
}
let instanceList = options[frontend]
if (instanceList === undefined) break
if (instanceList.length === 0) return null
if (initiator && instanceList.includes(initiator.origin)) {
if (type != "main_frame") return null
else return "BYPASSTAB"
}
randomInstance = utils.getRandomInstance(instanceList)
if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") {
randomInstance = `http://${frontend}.localhost:8080`
}
break
}
if (!frontend) return
return rewrite(url, frontend, randomInstance)
}
/** /**
* @param {URL} url * @param {URL} url
* @param {*} returnFrontend * @param {*} returnFrontend