Skip to content

Commit

Permalink
Fixing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Sep 3, 2024
1 parent 9d95c26 commit 51686a3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
38 changes: 14 additions & 24 deletions src/assets/javascripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function rewrite(url, originUrl, frontend, randomInstance) {
case "searx":
case "searxng":
for (const key of [...url.searchParams.keys()]) if (key !== "q") url.searchParams.delete(key)
console.log(url.searchParams)
return `${randomInstance}/${url.search}`
case "whoogle":
for (const key of [...url.searchParams.keys()]) if (key !== "q") url.searchParams.delete(key)
Expand Down Expand Up @@ -658,7 +657,7 @@ function computeService(url) {
return
} else {
for (const frontend in config.services[service].frontends) {
if (all(service, frontend, options, config).includes(utils.protocolHost(url))) {
if (all(service, frontend, options, config).findIndex(instance => url.href.startsWith(instance)) >= 0) {
return resolve(service)
}
}
Expand All @@ -670,8 +669,8 @@ function computeService(url) {
export function computeFrontend(url) {
for (const service in config.services) {
for (const frontend in config.services[service].frontends) {
if (all(service, frontend, options, config).includes(utils.protocolHost(url))) {
return {service, frontend}
if (all(service, frontend, options, config).findIndex(instance => url.href.startsWith(instance)) >= 0) {
return { service, frontend }
}
}
}
Expand All @@ -692,25 +691,20 @@ function switchInstance(url, customService) {
if (instancesList !== undefined) {
const newInstance = utils.getNextInstance(url.origin, instancesList)
if (newInstance) {
resolve(`${newInstance}${url.pathname}${url.search}`)
return
return resolve(`${newInstance}${url.pathname}${url.search}`)
}
}
} else {
for (const service in config.services) {
let instancesList = options[options[service].frontend]
if (instancesList === undefined) continue
if (!instancesList.includes(protocolHost)) continue

instancesList.splice(instancesList.indexOf(protocolHost), 1)
if (instancesList.length === 0) {
resolve()
return
}
const index = instancesList.findIndex(instance => url.href.startsWith(instance))
if (index < 0) continue
instancesList.splice(index, 1)
if (instancesList.length === 0) return resolve()
const newInstance = utils.getNextInstance(url.origin, instancesList)
if (newInstance) {
resolve(`${newInstance}${url.pathname}${url.search}`)
return
return resolve(`${newInstance}${url.pathname}${url.search}`)
}
}
}
Expand All @@ -724,11 +718,14 @@ function switchInstance(url, customService) {
async function reverse(url) {
let options = await utils.getOptions()
let config = await utils.getConfig()
let protocolHost = utils.protocolHost(url)
for (const service in config.services) {
let frontend = options[service].frontend
if (options[frontend] == undefined) continue
if (!options[frontend].includes(protocolHost) && protocolHost != `http://${frontend}.localhost:8080`) continue
if (
options[frontend].findIndex(instance => url.href.startsWith(instance)) < 0 &&
!url.href.startsWith(`http://${frontend}.localhost:8080`)
)
continue
switch (service) {
case "youtube":
case "imdb":
Expand Down Expand Up @@ -776,7 +773,6 @@ async function reverse(url) {
}

const defaultInstances = {
// invidious: ["https://inv.vern.cc"],
materialious: ["https://app.materialio.us"],
viewtube: ["https://viewtube.io"],
piped: ["https://pipedapi-libre.kavin.rocks"],
Expand All @@ -786,8 +782,6 @@ const defaultInstances = {
poketube: ["https://poketube.fun"],
proxiTok: ["https://proxitok.pabloferreiro.es"],
redlib: ["https://safereddit.com"],
libreddit: ["https://libreddit.spike.codes"],
teddit: ["https://teddit.net"],
eddrit: ["https://eddrit.com"],
scribe: ["https://scribe.rip"],
libMedium: ["https://md.vern.cc"],
Expand All @@ -807,15 +801,11 @@ const defaultInstances = {
intellectual: ["https://intellectual.insprill.net"],
ruralDictionary: ["https://rd.vern.cc"],
anonymousOverflow: ["https://code.whatever.social"],
biblioReads: ["https://biblioreads.ml"],
wikiless: ["https://wikiless.org"],
suds: ["https://sd.vern.cc"],
unfunny: ["https://uf.vern.cc"],
soprano: ["https://sp.vern.cc"],
meme: ["https://mm.vern.cc"],
waybackClassic: ["https://wayback-classic.net"],
gothub: ["https://gh.odyssey346.dev"],
mikuInvidious: ["https://mikuinv.resrv.org"],
tent: ["https://tent.sny.sh"],
wolfreeAlpha: ["https://gqq.gitlab.io", "https://uqq.gitlab.io"],
laboratory: ["https://lab.vern.cc"],
Expand Down
34 changes: 25 additions & 9 deletions src/pages/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ browser.webRequest.onBeforeRequest.addListener(
tabIdRedirects[details.tabId]
)

if (newUrl && newUrl.startsWith("https://no-instance.libredirect.invalid")) {
const url = new URL(newUrl)
const frontend = url.searchParams.get("frontend")
const oldUrl = new URL(url.searchParams.get("url"))
if (
(newUrl && newUrl.startsWith("https://no-instance.libredirect.invalid")) ||
(!newUrl && url.href.startsWith("https://no-instance.libredirect.invalid"))
) {
newUrl = newUrl ? new URL(newUrl) : url
const frontend = newUrl.searchParams.get("frontend")
const oldUrl = new URL(newUrl.searchParams.get("url"))
const params = new URLSearchParams({
message: "no_instance",
url: oldUrl,
frontend: frontend,
})
newUrl = browser.runtime.getURL(`/pages/messages/index.html?${params.toString()}`)
browser.tabs.update({
url: browser.runtime.getURL(`/pages/messages/index.html?${params.toString()}`),
})
return { cancel: true }
}

if (!newUrl) {
Expand All @@ -85,7 +91,10 @@ browser.webRequest.onBeforeRequest.addListener(
message: "disabled",
url: url.href,
})
newUrl = browser.runtime.getURL(`/pages/messages/index.html?${params.toString()}`)
browser.tabs.update({
url: browser.runtime.getURL(`/pages/messages/index.html?${params.toString()}`),
})
return { cancel: true }
}
}

Expand Down Expand Up @@ -121,9 +130,11 @@ browser.webRequest.onHeadersReceived.addListener(
frontend: frontend,
service: service,
})
browser.tabs.update({
url: browser.runtime.getURL(`/pages/messages/index.html?${params.toString()}`),
})
setTimeout(() => {
browser.tabs.update({
url: browser.runtime.getURL(`/pages/messages/index.html?${params.toString()}`),
})
}, 2000)
}
},
{ urls: ["<all_urls>"] }
Expand Down Expand Up @@ -365,3 +376,8 @@ browser.runtime.getPlatformInfo(r => {
})
}
})

browser.runtime.onMessage.addListener(r => {
if (r.message == "reverse") tabIdRedirects[r.tabId] = false
else if (r.message == "redirect") tabIdRedirects[r.tabId] = true
})
17 changes: 11 additions & 6 deletions src/pages/messages_src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
await servicesHelper.initDefaults()
opts = await utils.getOptions()
}
redirects = await utils.getList(opts)
options.set(opts)
config.set(await utils.getConfig())
})
Expand All @@ -45,18 +44,21 @@
$: if (_options) style = utils.style(_options, window)
let autoPicking = false
let redirects
const params = new URLSearchParams(window.location.search)
const oldUrl = new URL(params.get("url"))
async function autoPick() {
const frontend = params.get("frontend")
autoPicking = true
const redirects = await utils.getList(_options)
const instances = utils.randomInstances(redirects[frontend]["clearnet"], 5)
const pings = await Promise.all([...instances.map(async instance => [instance, await utils.ping(instance)])])
const pings = await Promise.all([
...instances.map(async instance => {
return [instance, await utils.ping(instance)]
}),
])
pings.sort((a, b) => a[1] - b[1])
console.log(pings)
_options[frontend].push(pings[0][0])
options.set(_options)
autoPicking = false
Expand Down Expand Up @@ -85,17 +87,20 @@
}
async function removeInstance() {
const service = await servicesHelper.computeService(oldUrl)
const frontend = params.get("frontend")
const i = _options[frontend].indexOf(utils.protocolHost(oldUrl))
const i = _options[frontend].findIndex(instance => oldUrl.href.startsWith(instance))
_options[frontend].splice(i, 1)
options.set(_options)
const newUrl = await servicesHelper.switchInstance(oldUrl, service)
browser.tabs.update({ url: newUrl })
}
async function removeAndAutoPickInstance() {
const service = await servicesHelper.computeService(oldUrl)
const frontend = params.get("frontend")
const i = _options[frontend].indexOf(utils.protocolHost(oldUrl))
const i = _options[frontend].findIndex(instance => oldUrl.href.startsWith(instance))
_options[frontend].splice(i, 1)
options.set(_options)
await autoPick()
Expand Down
17 changes: 11 additions & 6 deletions src/pages/popup_src/Buttons.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
<Row
class="interactive"
on:click={() => {
browser.tabs.update({ url: redirect }, () => {
window.close()
browser.tabs.query({ active: true, currentWindow: true }, tabs => {
browser.runtime.sendMessage({ message: "redirect", tabId: tabs[0].id }, () => {
browser.tabs.update({ url: redirect })
})
})
}}
>
Expand Down Expand Up @@ -74,10 +76,13 @@
</Row>
<Row
class="interactive"
on:click={() =>
browser.tabs.update({ url: redirectToOriginal }, () => {
window.close()
})}
on:click={() => {
browser.tabs.query({ active: true, currentWindow: true }, tabs => {
browser.runtime.sendMessage({ message: "reverse", tabId: tabs[0].id }, () => {
browser.tabs.update({ url: redirectToOriginal })
})
})
}}
>
<Label>{browser.i18n.getMessage("redirectToOriginal" || "Redirect to Original")}</Label>
<RedirectToOriginalIcon />
Expand Down

0 comments on commit 51686a3

Please sign in to comment.