Add almost all irregular url structures

This commit is contained in:
Hygna 2022-09-08 21:04:25 +01:00
parent f360a6ff1f
commit 4492914535
1 changed files with 152 additions and 120 deletions

View File

@ -50,7 +50,7 @@ init()
browser.storage.onChanged.addListener(init)
function redirect(url, type, initiator) {
let randomInstance
let randomInstance, frontend
if (url.pathname == "/") return
for (service in config.services) {
if (disabled && !disableOverride) continue
@ -58,14 +58,14 @@ function redirect(url, type, initiator) {
//if (!targets.some(rx => rx.test(url.href))) continue
if (!target.test(url)) continue
if (type != redirectType && type != "both") continue
for (frontend in service.frontends) {
browser.storage.local.get(`${service}Frontend`, (frontend = r[service + "Frontend"]))
let instanceList = redirects[frontend][curNetwork]
if (instanceList.length === 0 && networkFallback) instanceList = redirects[frontend].clearnet
if (instanceList.length === 0) return
randomInstance = utils.getRandomInstance(instanceList)
}
}
switch (frontend) {
// This is where all instance-specific code must be ran to convert the service url to one that can be understood by the frontend.
case "beatbump":
return `${randomInstance}${url.pathname}${url.search}`
.replace("/watch?v=", "/listen?id=")
@ -74,9 +74,43 @@ function redirect(url, type, initiator) {
.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/") + "?filter=song")
case "hyperpipe":
return `${randomInstance}${url.pathname}${url.search}`.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/"))
case "bibliogram":
const reservedPaths = ["u", "p", "privacy"]
if (url.pathname === "/" || reservedPaths.includes(url.pathname.split("/")[1])) return `${randomInstance}${url.pathname}${url.search}`
if (url.pathname.startsWith("/reel") || url.pathname.startsWith("/tv")) return `${randomInstance}/p${url.pathname.replace(/\/reel|\/tv/i, "")}${url.search}`
else return `${randomInstance}/u${url.pathname}${url.search}` // Likely a user profile, redirect to '/u/...'
case "lbryDesktop":
return url.href.replace(/^https?:\/{2}odysee\.com\//, "lbry://").replace(/:(?=[a-zA-Z0-9])/g, "#")
case "neuters":
if (url.pathname.startsWith("/article/") || url.pathname.startsWith("/pf/") || url.pathname.startsWith("/arc/") || url.pathname.startsWith("/resizer/")) return null
else if (url.pathname.endsWith("/")) return `${randomInstance}${url.pathname}`
else return `${randomInstance}${url.pathname}/`
case "searx":
case "searxng":
return `${randomInstance}/?q=${encodeURIComponent(url.searchParams.get("q"))}`
case "whoogle":
return `${randomInstance}/search${encodeURIComponent(url.searchParams.get("q"))}`
case "librex":
return `${randomInstance}/search.php${encodeURIComponent(url.searchParams.get("q"))}`
case "send":
return randomInstance
case "nitter":
if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") {
const [, id, format, extra] = url.search.match(/(.*)\?format=(.*)&(.*)/)
const query = encodeURIComponent(`${id}.${format}?${extra}`)
return `${randomInstance}/pic${url.pathname}${query}`
} else if (url.pathname.split("/").includes("tweets")) return `${randomInstance}${url.pathname.replace("/tweets", "")}${url.search}`
else if (url.host == "t.co") return `${randomInstance}/t.co${url.pathname}`
else return `${randomInstance}${url.pathname}${url.search}`
case "yatte":
return url.href.replace(/^https?:\/{2}/, "yattee://")
case "freetube":
return `freetube://https://youtube.com${url.pathname}${url.search}`
case "simplyTranslate":
return `${randomInstance}/${url.search}`
default:
return `${randomInstance}${url.pathname}${url.search}`
}
} // TODO: Wikiless, All youtube frontends by changing regex, lingva
}
function initDefaults() {
@ -88,27 +122,25 @@ function initDefaults() {
redirects = dataJson
browser.storage.local.get(["cloudflareBlackList", "authenticateBlackList", "offlineBlackList"], async r => {
for (service in config.services) {
for (defaultOption in service.defaults) {
browser.storage.local.set({
[defaultOption]: defaultOption.value,
})
}
for (frontend in service.frontends) {
for (const instance of [...r.cloudflareBlackList, ...r.authenticateBlackList, ...r.offlineBlackList]) {
let i = redirects[frontend]["clearnet"].indexOf(instance)
if (i > -1) redirects[frontend]["clearnet"].splice(i, 1)
}
for (network in config.networks) {
browser.storage.local.set({
["disable" + camelCase(service)]: false,
[service + "Redirects"]: redirects,
[service + "RedirectType"]: "both",
})
for (frontend in service.frontends) {
for (protocol in config.protocols) {
browser.storage.local.set({
[frontend + camelCase(protocol) + "RedirectsChecks"]: [...redirects[frontend][protocol]],
[frontend + camelCase(protocol) + "CustomRedirects"]: [],
[frontend + camelCase(network) + "RedirectsChecks"]: [...redirects[frontend][network]],
[frontend + camelCase(network) + "CustomRedirects"]: [],
})
}
}
}
;() => resolve()
}
}
})
})
})