Cleaning code

This commit is contained in:
ManeraKai 2024-05-20 13:57:31 +03:00
parent 1b529ed392
commit 9d750897ec
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
4 changed files with 63 additions and 145 deletions

View File

@ -5,12 +5,9 @@ window.browser = window.browser || window.chrome
let config, options let config, options
function init() { async function init() {
return new Promise(async resolve => { options = await utils.getOptions()
options = await utils.getOptions() config = await utils.getConfig()
config = await utils.getConfig()
resolve()
})
} }
init() init()
@ -69,38 +66,26 @@ async function redirectAsync(url, type, initiator, forceRedirection) {
*/ */
function rewrite(url, frontend, randomInstance) { function rewrite(url, frontend, randomInstance) {
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}`
}
case "4get": { case "4get": {
const s = url.searchParams.get("q") const s = url.searchParams.get("q")
if (s !== null) { if (s !== null) return `${randomInstance}/web?s=${encodeURIComponent(s)}`
return `${randomInstance}/web?s=${encodeURIComponent(s)}`
}
return randomInstance return randomInstance
} }
case "librey": { case "librey":
return `${randomInstance}/search.php${url.search}` return `${randomInstance}/search.php${url.search}`
} case "yattee":
case "send": {
return randomInstance
}
case "yattee": {
return url.href.replace(/^https?:\/{2}/, "yattee://") return url.href.replace(/^https?:\/{2}/, "yattee://")
} case "freetube":
case "freetube": {
return 'freetube://' + url.href return 'freetube://' + url.href
} 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)
@ -123,19 +108,13 @@ function rewrite(url, frontend, randomInstance) {
} }
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
} }
case "simplyTranslate": { case "simplyTranslate":
return `${randomInstance}/${url.search}` return `${randomInstance}/${url.search}`
} case "send":
case "mozhi": { case "mozhi":
return `${randomInstance}` return randomInstance
} case "libreTranslate":
case "libreTranslate": { return `${randomInstance}/${url.search.replace("sl", "source").replace("tl", "target").replace("text", "q")}`
let search = url.search
.replace("sl", "source")
.replace("tl", "target")
.replace("text", "q")
return `${randomInstance}/${search}`
}
case "osm": { case "osm": {
const placeRegex = /\/place\/(.*?)\// const placeRegex = /\/place\/(.*?)\//
function convertMapCentre(url) { function convertMapCentre(url) {
@ -249,12 +228,9 @@ function rewrite(url, frontend, randomInstance) {
} }
return `${randomInstance}${wiki}${urlpath}${url.search}` return `${randomInstance}${wiki}${urlpath}${url.search}`
} }
case "rimgo": { case "rimgo":
if (url.href.search(/^https?:\/{2}(?:[im]\.)?stack\./) > -1) { if (url.href.search(/^https?:\/{2}(?:[im]\.)?stack\./) > -1) return `${randomInstance}/stack${url.pathname}${url.search}`
return `${randomInstance}/stack${url.pathname}${url.search}`
}
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
}
case "redlib": case "redlib":
case "libreddit": { case "libreddit": {
const subdomain = url.hostname.match(/^(?:(?:external-)?preview|i)(?=\.redd\.it)/) const subdomain = url.hostname.match(/^(?:(?:external-)?preview|i)(?=\.redd\.it)/)
@ -269,13 +245,12 @@ function rewrite(url, frontend, randomInstance) {
} }
return randomInstance return randomInstance
} }
case "teddit": { case "teddit":
if (/^(?:(?:external-)?preview|i)\.redd\.it/.test(url.hostname)) { if (/^(?:(?:external-)?preview|i)\.redd\.it/.test(url.hostname)) {
if (url.search == "") return `${randomInstance}${url.pathname}?teddit_proxy=${url.hostname}` if (url.search == "") return `${randomInstance}${url.pathname}?teddit_proxy=${url.hostname}`
else return `${randomInstance}${url.pathname}${url.search}&teddit_proxy=${url.hostname}` else return `${randomInstance}${url.pathname}${url.search}&teddit_proxy=${url.hostname}`
} }
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
}
case "neuters": { case "neuters": {
const p = url.pathname const p = url.pathname
if (p.startsWith('/article/') || p.startsWith('/pf/') || p.startsWith('/arc/') || p.startsWith('/resizer/')) { if (p.startsWith('/article/') || p.startsWith('/pf/') || p.startsWith('/arc/') || p.startsWith('/resizer/')) {
@ -283,19 +258,14 @@ function rewrite(url, frontend, randomInstance) {
} }
return `${randomInstance}${p}` return `${randomInstance}${p}`
} }
case "dumb": { case "dumb":
if (url.pathname.endsWith('-lyrics')) { if (url.pathname.endsWith('-lyrics')) return `${randomInstance}${url.pathname}`
return `${randomInstance}${url.pathname}`
}
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
} case "intellectual":
case "intellectual": {
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
} case "ruralDictionary":
case "ruralDictionary": {
if (!url.pathname.includes('/define.php') && !url.pathname.includes('/random.php') && url.pathname != '/') return randomInstance if (!url.pathname.includes('/define.php') && !url.pathname.includes('/random.php') && url.pathname != '/') return randomInstance
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
}
case "anonymousOverflow": { case "anonymousOverflow": {
if (url.hostname == "stackoverflow.com") { if (url.hostname == "stackoverflow.com") {
const threadID = /^\/a\/(\d+)\/?/.exec(url.pathname) const threadID = /^\/a\/(\d+)\/?/.exec(url.pathname)
@ -317,9 +287,8 @@ function rewrite(url, frontend, randomInstance) {
} }
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
} }
case "biblioReads": { case "biblioReads":
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
}
case "wikiless": { case "wikiless": {
let hostSplit = url.host.split(".") let hostSplit = url.host.split(".")
// wikiless doesn't have mobile view support yet // wikiless doesn't have mobile view support yet
@ -330,10 +299,9 @@ function rewrite(url, frontend, randomInstance) {
} }
return `${randomInstance}${url.pathname}${url.search}${url.hash}` return `${randomInstance}${url.pathname}${url.search}${url.hash}`
} }
case "proxiTok": { case "proxiTok":
if (url.pathname.startsWith('/email')) return randomInstance if (url.pathname.startsWith('/email')) return randomInstance
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
}
case "waybackClassic": { case "waybackClassic": {
const regex = /^\/\web\/(?:[0-9]+)?\*\/(.*)/.exec(url.pathname) const regex = /^\/\web\/(?:[0-9]+)?\*\/(.*)/.exec(url.pathname)
if (regex) { if (regex) {
@ -348,19 +316,13 @@ function rewrite(url, frontend, randomInstance) {
} }
return return
} }
case "gothub": { case "gothub":
if (url.hostname == "gist.github.com") return `${randomInstance}/gist${url.pathname}${url.search}` if (url.hostname == "gist.github.com") return `${randomInstance}/gist${url.pathname}${url.search}`
if (url.hostname == "raw.githubusercontent.com") return `${randomInstance}/raw${url.pathname}${url.search}` if (url.hostname == "raw.githubusercontent.com") return `${randomInstance}/raw${url.pathname}${url.search}`
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
} case "mikuInvidious":
case "mikuInvidious": { if (url.hostname == "bilibili.com" || url.hostname == "www.bilibili.com" || url.hostname == 'b23.tv') return `${randomInstance}${url.pathname}${url.search}`
if (url.hostname == "bilibili.com" || url.hostname == "www.bilibili.com" || url.hostname == 'b23.tv') { if (url.hostname == "space.bilibili.com") return `${randomInstance}/space${url.pathname}${url.search}`
return `${randomInstance}${url.pathname}${url.search}`
}
if (url.hostname == "space.bilibili.com") {
return `${randomInstance}/space${url.pathname}${url.search}`
}
}
case "tent": { case "tent": {
if (url.hostname == 'bandcamp.com' && url.pathname == '/search') { if (url.hostname == 'bandcamp.com' && url.pathname == '/search') {
const query = url.searchParams.get('q') const query = url.searchParams.get('q')
@ -396,9 +358,8 @@ function rewrite(url, frontend, randomInstance) {
} }
} }
} }
case "binternet": { case "binternet":
if (url.hostname == "i.pinimg.com") return `${randomInstance}/image_proxy.php?url=${url.href}` if (url.hostname == "i.pinimg.com") return `${randomInstance}/image_proxy.php?url=${url.href}`
}
case "laboratory": { case "laboratory": {
let path = url.pathname let path = url.pathname
if (path == "/") path = "" if (path == "/") path = ""
@ -446,68 +407,44 @@ function rewrite(url, frontend, randomInstance) {
} }
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
} }
case "tuboYoutube": { case "tuboYoutube":
if (url.pathname.startsWith("/channel")) { if (url.pathname.startsWith("/channel")) return `${randomInstance}/channel?url=${encodeURIComponent(url.href)}`
return `${randomInstance}/channel?url=${encodeURIComponent(url.href)}` if (url.pathname.startsWith("/watch")) return `${randomInstance}/stream?url=${encodeURIComponent(url.href)}`
} return randomInstance
if (url.pathname.startsWith("/watch")) { case "tuboSoundcloud":
return `${randomInstance}/stream?url=${encodeURIComponent(url.href)}`
}
return `${randomInstance}`
}
case "tuboSoundcloud": {
if (url.pathname == '/') return `${randomInstance}?kiosk?serviceId=1` if (url.pathname == '/') return `${randomInstance}?kiosk?serviceId=1`
if (url.pathname.match(/^\/[^\/]+(\/$|$)/)) { if (url.pathname.match(/^\/[^\/]+(\/$|$)/)) return `${randomInstance}/channel?url=${encodeURIComponent(url.href)}`
return `${randomInstance}/channel?url=${encodeURIComponent(url.href)}` if (url.pathname.match(/^\/[^\/]+\/[^\/]+/)) return `${randomInstance}/stream?url=${encodeURIComponent(url.href)}`
} return randomInstance
if (url.pathname.match(/^\/[^\/]+\/[^\/]+/)) {
return `${randomInstance}/stream?url=${encodeURIComponent(url.href)}`
}
return `${randomInstance}`
}
case "twineo": case "twineo":
case "safetwitch": { case "safetwitch":
if (url.hostname.startsWith("clips.")) { if (url.hostname.startsWith("clips.")) return `${randomInstance}/clip${url.pathname}${url.search}`
return `${randomInstance}/clip${url.pathname}${url.search}`
}
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
}
case "tekstoLibre": { case "tekstoLibre":
return `${randomInstance}/?${url.pathname.slice(1)}`; return `${randomInstance}/?${url.pathname.slice(1)}`;
} case "skyview":
case "skyview": {
if (url.pathname == '/') return randomInstance if (url.pathname == '/') return randomInstance
return `${randomInstance}?url=${encodeURIComponent(url.href)}` return `${randomInstance}?url=${encodeURIComponent(url.href)}`
}
case "priviblur": { case "priviblur": {
if (url.hostname == "www.tumblr.com") if (url.hostname == "www.tumblr.com") return `${randomInstance}${url.pathname}${url.search}`
return `${randomInstance}${url.pathname}${url.search}` if (url.hostname.startsWith("assets")) return `${randomInstance}/tblr/assets${url.pathname}${url.search}`
if (url.hostname.startsWith("static")) return `${randomInstance}/tblr/static${url.pathname}${url.search}`
if (url.hostname.startsWith("assets"))
return `${randomInstance}/tblr/assets${url.pathname}${url.search}`
if (url.hostname.startsWith("static"))
return `${randomInstance}/tblr/static${url.pathname}${url.search}`
const reg = /^([0-9]+)\.media\.tumblr\.com/.exec(url.hostname) // *.media.tumblr.com const reg = /^([0-9]+)\.media\.tumblr\.com/.exec(url.hostname) // *.media.tumblr.com
if (reg) if (reg) return `${randomInstance}/tblr/media/${reg[1]}${url.pathname}${url.search}`
return `${randomInstance}/tblr/media/${reg[1]}${url.pathname}${url.search}`
const blogregex = /^(?:www\.)?([a-z\d-]+)\.tumblr\.com/.exec(url.hostname) // <blog>.tumblr.com const blogregex = /^(?:www\.)?([a-z\d-]+)\.tumblr\.com/.exec(url.hostname) // <blog>.tumblr.com
if (blogregex) { if (blogregex) {
const blog_name = blogregex[1]; const blog_name = blogregex[1];
// Under the <blog>.tumblr.com domain posts are under a /post path // Under the <blog>.tumblr.com domain posts are under a /post path
if (url.pathname.startsWith("/post")) { if (url.pathname.startsWith("/post")) return `${randomInstance}/${blog_name}${url.pathname.slice(5)}${url.search}`
return `${randomInstance}/${blog_name}${url.pathname.slice(5)}${url.search}` else return `${randomInstance}/${blog_name}${url.pathname}${url.search}`;
} else {
return `${randomInstance}/${blog_name}${url.pathname}${url.search}`;
}
} }
return `${randomInstance}${url.pathname}${url.search}`; return `${randomInstance}${url.pathname}${url.search}`;
} }
default: { default:
return `${randomInstance}${url.pathname}${url.search}` return `${randomInstance}${url.pathname}${url.search}`
}
} }
} }
@ -657,10 +594,11 @@ async function reverse(url) {
case "quora": case "quora":
case "medium": case "medium":
return `${config.services[service].url}${url.pathname}${url.search}` return `${config.services[service].url}${url.pathname}${url.search}`
case "fandom": case "fandom": {
let regex = url.pathname.match(/^\/([a-zA-Z0-9-]+)\/wiki\/(.*)/) let regex = url.pathname.match(/^\/([a-zA-Z0-9-]+)\/wiki\/(.*)/)
if (regex) return `https://${regex[1]}.fandom.com/wiki/${regex[2]}` if (regex) return `https://${regex[1]}.fandom.com/wiki/${regex[2]}`
return return
}
case "wikipedia": { case "wikipedia": {
const lang = url.searchParams.get("lang") const lang = url.searchParams.get("lang")
if (lang != null) { if (lang != null) {
@ -678,9 +616,8 @@ async function reverse(url) {
} }
return return
} }
case "tekstowo": { case "tekstowo":
return `${config.services[service].url}/${url.search.slice(1)}` return `${config.services[service].url}/${url.search.slice(1)}`
}
default: default:
return return
} }
@ -875,8 +812,7 @@ function isException(url) {
if (exceptions.url) { if (exceptions.url) {
for (let item of exceptions.url) { for (let item of exceptions.url) {
item = new URL(item) item = new URL(item)
item = item.href item = item.href.replace(/^http:\/\//, 'https://')
item = item.replace(/^http:\/\//, 'https://')
if (item == url.href) return true if (item == url.href) return true
} }
} }

View File

@ -15,13 +15,8 @@ function getRandomInstance(instances) {
*/ */
function getNextInstance(currentInstanceUrl, instances) { function getNextInstance(currentInstanceUrl, instances) {
const currentInstanceIndex = instances.indexOf(currentInstanceUrl); const currentInstanceIndex = instances.indexOf(currentInstanceUrl);
if (currentInstanceIndex === -1) return getRandomInstance(instances);
if (currentInstanceIndex === -1){
return getRandomInstance(instances);
}
const nextInstanceIndex = (currentInstanceIndex + 1) % instances.length; const nextInstanceIndex = (currentInstanceIndex + 1) % instances.length;
return instances[nextInstanceIndex]; return instances[nextInstanceIndex];
} }
@ -101,12 +96,8 @@ function getPingCache() {
function getBlacklist(options) { function getBlacklist(options) {
return new Promise(resolve => { return new Promise(resolve => {
let url let url
if (options.fetchInstances == 'github') { if (options.fetchInstances == 'github') url = 'https://raw.githubusercontent.com/libredirect/instances/main/blacklist.json'
url = 'https://raw.githubusercontent.com/libredirect/instances/main/blacklist.json' else if (options.fetchInstances == 'codeberg') url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/blacklist.json'
}
else if (options.fetchInstances == 'codeberg') {
url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/blacklist.json'
}
else { else {
resolve('disabled') resolve('disabled')
return return
@ -134,12 +125,8 @@ function getBlacklist(options) {
function getList(options) { function getList(options) {
return new Promise(resolve => { return new Promise(resolve => {
let url let url
if (options.fetchInstances == 'github') { if (options.fetchInstances == 'github') url = 'https://raw.githubusercontent.com/libredirect/instances/main/data.json'
url = 'https://raw.githubusercontent.com/libredirect/instances/main/data.json' else if (options.fetchInstances == 'codeberg') url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/data.json'
}
else if (options.fetchInstances == 'codeberg') {
url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/data.json'
}
else { else {
resolve('disabled') resolve('disabled')
return return

View File

@ -260,7 +260,7 @@ async function processCustomInstances(frontend, document) {
/** /**
* @param {string} frontend * @param {string} frontend
* @param {*} networks * @param {*} networks
* @param {*} document * @param {Document} document
* @param {*} redirects * @param {*} redirects
* @param {*} blacklist * @param {*} blacklist
*/ */
@ -393,7 +393,5 @@ function processTime(time) {
color = "red" color = "red"
text = 'Server not found' text = 'Server not found'
} }
return { return { color, text }
color, text
}
} }

View File

@ -206,10 +206,7 @@ document.getElementById("custom-exceptions-instance-form").addEventListener("sub
if (val) { if (val) {
options = await utils.getOptions() options = await utils.getOptions()
options.exceptions = exceptionsCustomInstances options.exceptions = exceptionsCustomInstances
browser.storage.local.set({ options }, () => browser.storage.local.set({ options }, () => nameCustomInstanceInput.value = "")
nameCustomInstanceInput.value = ""
)
} }
calcExceptionsCustomInstances() calcExceptionsCustomInstances()
}) })