Started work on instance switching

This commit is contained in:
Hygna 2022-09-25 17:27:43 +01:00
parent 7e8c13bb3b
commit 03f242fe11
6 changed files with 77 additions and 87 deletions

View File

@ -30,7 +30,7 @@ async function initDefaults() {
regex: [],
},
theme: "DEFAULT",
popupFrontends: ["youtube", "twitter", "instagram", "tiktok", "imgur", "reddit", "quora", "translate", "maps"],
popupServices: ["youtube", "twitter", "instagram", "tiktok", "imgur", "reddit", "quora", "translate", "maps"],
autoRedirect: false,
firstPartyIsolate: false,
network: "clearnet",
@ -42,29 +42,7 @@ async function initDefaults() {
)
}
const allPopupFrontends = [
"youtube",
"youtubeMusic",
"twitter",
"instagram",
"tiktok",
"imgur",
"reddit",
"search",
"translate",
"maps",
"wikipedia",
"medium",
"quora",
"imdb",
"reuters",
"peertube",
"lbry",
"sendTargets",
]
export default {
isException,
initDefaults,
allPopupFrontends,
}

View File

@ -19,7 +19,7 @@ async function getConfig() {
}
function init() {
return new Promise(async resolve => {
return new Promise(resolve => {
browser.storage.local.get(["network", "networkFallback", "redirects"], r => {
options.network = r.network
options.networkFallback = r.networkFallback
@ -401,7 +401,7 @@ function initDefaults() {
.then(response => response.text())
.then(async data => {
let dataJson = JSON.parse(data)
redirects = JSON.parse(data)
let tmpRedirects = JSON.parse(data)
browser.storage.local.get(["cloudflareBlackList", "authenticateBlackList", "offlineBlackList"], async r => {
for (const service in config.services) {
if (config.services[service].targets == "datajson") {
@ -413,7 +413,7 @@ function initDefaults() {
}
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) {
let clearnetChecks = redirects[frontend].clearnet
let clearnetChecks = tmpRedirects[frontend].clearnet
for (const instance of [...r.cloudflareBlackList, ...r.authenticateBlackList, ...r.offlineBlackList]) {
let i = clearnetChecks.indexOf(instance)
if (i > -1) clearnetChecks.splice(i, 1)
@ -428,7 +428,7 @@ function initDefaults() {
break
default:
browser.storage.local.set({
[frontend + utils.camelCase(network) + "RedirectsChecks"]: [...redirects[frontend][network]],
[frontend + utils.camelCase(network) + "RedirectsChecks"]: [...tmpRedirects[frontend][network]],
[frontend + utils.camelCase(network) + "CustomRedirects"]: [],
})
}
@ -436,11 +436,11 @@ function initDefaults() {
}
}
}
browser.storage.local.set({
redirects: dataJson,
})
resolve()
})
browser.storage.local.set({
redirects: dataJson,
})
resolve()
})
})
}
@ -456,8 +456,39 @@ function computeService(url) {
return null
}
function switchInstance(url) {
return new Promise(async resolve => {
await getConfig()
await init()
for (const service in config.services) {
if (!options[service].enabled) continue
const protocolHost = utils.protocolHost(url)
if (!all(service).includes(protocolHost)) continue
let instancesList = [...options[options[service].frontend][options.network].checks, ...options[options[service].frontend][options.network].custom]
if (instancesList.length === 0 && options.networkFallback) instancesList = [...options[options[service].frontend].clearnet.checks, ...options[options[service].frontend].clearnet.custom]
let oldInstance
const i = instancesList.indexOf(protocolHost)
if (i > -1) {
oldInstance = instancesList[i]
instancesList.splice(i, 1)
}
if (instancesList.length === 0) {
resolve()
return
}
const randomInstance = utils.getRandomInstance(instancesList)
const oldUrl = `${oldInstance}${url.pathname}${url.search}`
// This is to make instance switching work when the instance depends on the pathname, eg https://darmarit.org/searx
resolve(oldUrl.replace(oldUrl, randomInstance))
}
})
}
export default {
redirect,
initDefaults,
computeService,
switchInstance,
}

View File

@ -18,6 +18,7 @@ import reutersHelper from "./reuters.js"
import youtubeMusicHelper from "./youtubeMusic.js"
import mapsHelper from "./maps.js"
import localise from "./localise.js"
import servicesHelper from "./services.js"
function getRandomInstance(instances) {
return instances[~~(instances.length * Math.random())]
@ -479,22 +480,7 @@ function switchInstance(test) {
resolve()
return
}
let newUrl = await youtubeHelper.switchInstance(url, true)
if (!newUrl) newUrl = await twitterHelper.switchInstance(url, true)
if (!newUrl) newUrl = await instagramHelper.switchInstance(url, true)
if (!newUrl) newUrl = await redditHelper.switchInstance(url, true)
if (!newUrl) newUrl = await searchHelper.switchInstance(url, true)
if (!newUrl) newUrl = await translateHelper.switchInstance(url, true)
if (!newUrl) newUrl = await mediumHelper.switchInstance(url, true)
if (!newUrl) newUrl = await quoraHelper.switchInstance(url, true)
if (!newUrl) newUrl = await libremdbHelper.switchInstance(url, true)
if (!newUrl) newUrl = await tiktokHelper.switchInstance(url, true)
if (!newUrl) newUrl = await sendTargetsHelper.switchInstance(url, true)
if (!newUrl) newUrl = await peertubeHelper.switchInstance(url, true)
if (!newUrl) newUrl = await lbryHelper.switchInstance(url, true)
if (!newUrl) newUrl = await imgurHelper.switchInstance(url, true)
if (!newUrl) newUrl = await wikipediaHelper.switchInstance(url, true)
if (!newUrl) newUrl = await youtubeMusicHelper.switchInstance(url, true)
let newUrl = await servicesHelper.switchInstance(url)
if (newUrl) {
if (!test) browser.tabs.update({ url: newUrl })

View File

@ -2,12 +2,10 @@
import generalHelper from "../../assets/javascripts/general.js"
import utils from "../../assets/javascripts/utils.js"
import servicesHelper from "../../assets/javascripts/services.js"
window.browser = window.browser || window.chrome
browser.runtime.onInstalled.addListener(details => {
function initDefaults() {
fetch("/instances/blacklist.json")
@ -53,26 +51,6 @@ browser.webRequest.onBeforeRequest.addListener(
return null
}
/*
let newUrl = youtubeMusicHelper.redirect(url, details.type)
if (!newUrl) newUrl = youtubeHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = twitterHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = instagramHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = mapsHelper.redirect(url, initiator)
if (!newUrl) newUrl = redditHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = mediumHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = quoraHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = libremdbHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = reutersHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = imgurHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = tiktokHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = sendTargetsHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = peertubeHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = lbryHelper.redirect(url, details.type, initiator)
if (!newUrl) newUrl = translateHelper.redirect(url)
if (!newUrl) newUrl = searchHelper.redirect(url)
if (!newUrl) newUrl = wikipediaHelper.redirect(url)
*/
let newUrl = servicesHelper.redirect(url, details.type, initiator)
if (details.frameAncestors && details.frameAncestors.length > 0 && generalHelper.isException(new URL(details.frameAncestors[0].url))) newUrl = null

View File

@ -15,6 +15,20 @@ updateInstancesElement.addEventListener("click", async () => {
} else updateInstancesElement.innerHTML = "Failed Miserabely"
})
let config
async function getConfig() {
return new Promise(resolve => {
fetch("/config/config.json")
.then(response => response.text())
.then(data => {
const tmp = JSON.parse(data)
config = tmp.config
resolve()
})
})
}
let exportSettingsElement = document.getElementById("export-settings")
function exportSettings() {
@ -111,17 +125,20 @@ let nameCustomInstanceInput = document.getElementById("exceptions-custom-instanc
let instanceTypeElement = document.getElementById("exceptions-custom-instance-type")
let instanceType = "url"
let popupFrontends
for (const frontend of generalHelper.allPopupFrontends)
document.getElementById(frontend).addEventListener("change", event => {
if (event.target.checked && !popupFrontends.includes(frontend)) popupFrontends.push(frontend)
else if (popupFrontends.includes(frontend)) {
var index = popupFrontends.indexOf(frontend)
if (index !== -1) popupFrontends.splice(index, 1)
}
browser.storage.local.set({ popupFrontends })
})
let popupServices
await getConfig()
for (const service in config.services) {
document.getElementById(service).addEventListener("change", event => {
if (event.target.checked && !popupServices.includes(service)) popupServices.push(service)
else if (popupServices.includes(service)) {
var index = popupServices.indexOf(service)
if (index !== -1) popupServices.splice(index, 1)
}
browser.storage.local.set({ popupServices })
})
}
// const firstPartyIsolate = document.getElementById('firstPartyIsolate');
// firstPartyIsolate.addEventListener("change", () => browser.storage.local.set({ firstPartyIsolate: firstPartyIsolate.checked }))
@ -214,9 +231,9 @@ browser.storage.local.get(
calcExceptionsCustomInstances()
})
browser.storage.local.get("popupFrontends", r => {
popupFrontends = r.popupFrontends
for (const frontend of generalHelper.allPopupFrontends) document.getElementById(frontend).checked = popupFrontends.includes(frontend)
browser.storage.local.get("popupServices", r => {
popupServices = r.popupServices
for (const service in config.services) document.getElementById(service).checked = popupServices.includes(service)
})
}
)

View File

@ -69,12 +69,12 @@ function getEnabled() {
})
}
browser.storage.local.get("popupFrontends", r => {
browser.storage.local.get("popupServices", r => {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
for (const frontend of generalHelper.allPopupFrontends) {
if (!r.popupFrontends.includes(frontend)) allSites.getElementsByClassName(frontend)[0].classList.add("hide")
else allSites.getElementsByClassName(frontend)[0].classList.remove("hide")
currSite.getElementsByClassName(frontend)[0].classList.add("hide")
for (const service in config.services) {
if (!r.popupServices.includes(service)) allSites.getElementsByClassName(service)[0].classList.add("hide")
else allSites.getElementsByClassName(service)[0].classList.remove("hide")
currSite.getElementsByClassName(service)[0].classList.add("hide")
}
await getEnabled()