libredirect/src/pages/popup/popup.js

162 lines
5.5 KiB
JavaScript
Raw Normal View History

"use strict"
window.browser = window.browser || window.chrome
import utils from "../../assets/javascripts/utils.js"
2022-09-23 21:57:25 +02:00
import serviceHelper from "../../assets/javascripts/services.js"
2022-10-02 16:10:36 +02:00
let config,
divs = {}
2022-09-23 21:57:25 +02:00
async function getConfig() {
return new Promise(resolve => {
fetch("/config/config.json")
.then(response => response.text())
.then(data => {
config = JSON.parse(data)
2022-09-23 21:57:25 +02:00
resolve()
})
})
}
2022-09-23 21:57:25 +02:00
await getConfig()
2022-10-02 16:10:36 +02:00
utils.switchInstance(true).then(r => {
if (!r) document.getElementById("change_instance_div").style.display = "none"
else document.getElementById("change_instance").addEventListener("click", () => utils.switchInstance(false))
})
2022-10-03 07:44:49 +02:00
utils.copyRaw(true).then(r => {
2022-10-02 16:10:36 +02:00
if (!r) document.getElementById("copy_raw_div").style.display = "none"
else {
const copy_raw = document.getElementById("copy_raw")
copy_raw.addEventListener("click", () => utils.copyRaw(false, copy_raw))
}
})
document.getElementById("more-options").addEventListener("click", () => browser.runtime.openOptionsPage())
2022-09-23 21:57:25 +02:00
const allSites = document.getElementsByClassName("all_sites")[0]
const currSite = document.getElementsByClassName("current_site")[0]
2022-09-23 21:57:25 +02:00
function setDivs() {
return new Promise(resolve => {
divs.instance = document.getElementById("instance")
2022-09-23 21:57:25 +02:00
for (const service in config.services) {
divs[service] = {}
divs[service].toggle = {}
divs[service].current = currSite.getElementsByClassName(service)[0]
divs[service].all = allSites.getElementsByClassName(service)[0]
divs[service].toggle.current = currSite.getElementsByClassName(service + "-enabled")[0]
divs[service].toggle.all = allSites.getElementsByClassName(service + "-enabled")[0]
}
resolve()
})
}
2022-09-23 21:57:25 +02:00
await setDivs()
2019-10-07 13:59:31 +02:00
const currentSiteIsFrontend = document.getElementById("current_site_divider")
browser.storage.local.get(["options", "redirects"], r => {
2022-09-23 21:57:25 +02:00
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
document.getElementById("instance-div").classList.add("hide")
2022-10-02 17:44:11 +02:00
for (const service in config.services) {
2022-09-27 21:59:25 +02:00
if (!r.options.popupServices.includes(service)) allSites.getElementsByClassName(service)[0].classList.add("hide")
2022-09-25 18:27:43 +02:00
else allSites.getElementsByClassName(service)[0].classList.remove("hide")
currSite.getElementsByClassName(service)[0].classList.add("hide")
2022-09-23 21:57:25 +02:00
}
2022-09-27 21:59:25 +02:00
for (const service in config.services) {
divs[service].toggle.all.checked = r.options[service].enabled
divs[service].toggle.current.checked = r.options[service].enabled
}
2022-10-02 17:44:11 +02:00
2022-09-23 21:57:25 +02:00
let url
try {
url = new URL(tabs[0].url)
} catch {
currentSiteIsFrontend.classList.add("hide")
2022-09-24 12:44:18 +02:00
document.getElementById("unify_div").style.display = "none"
2022-09-23 21:57:25 +02:00
return
}
2022-10-02 17:44:11 +02:00
2022-10-02 16:10:36 +02:00
let service = await serviceHelper.computeService(url, true)
let frontend
let instance
2022-09-28 21:18:32 +02:00
if (service) {
2022-10-06 22:12:51 +02:00
if (typeof service != "string") {
instance = service[2]
2022-10-02 16:10:36 +02:00
frontend = service[1]
2022-10-02 17:44:11 +02:00
service = service[0]
divs.instance.innerHTML = instance.replace(/https?:\/{2}/, "")
let tmp
let instanceNetwork
for (const network in config.networks) {
tmp = r.redirects[frontend][network].indexOf(instance)
if (tmp > -1) {
const instanceDiv = document.getElementById("instance-enabled")
tmp = r.options[frontend][network].enabled.indexOf(instance)
if (tmp > -1) instanceDiv.checked = true
else instanceDiv.checked = false
instanceNetwork = network
instanceDiv.addEventListener("change", () => {
browser.storage.local.get("options", r => {
// Although options would be avaliable in this context, it is fetched again to make sure it is up to date
let options = r.options
if (instanceDiv.checked) options[frontend][instanceNetwork].enabled.push(instance)
else options[frontend][instanceNetwork].enabled.splice(options[frontend][instanceNetwork].enabled.indexOf(instance), 1)
browser.storage.local.set({ options })
})
})
break
}
}
document.getElementById("instance-div").classList.remove("hide")
2022-10-02 16:10:36 +02:00
}
2022-09-28 21:18:32 +02:00
divs[service].current.classList.remove("hide")
divs[service].all.classList.add("hide")
2022-10-03 07:44:49 +02:00
if (frontend && config.services[service].frontends[frontend].preferences && !config.services[service].frontends[frontend].preferences.token) {
2022-09-23 21:57:25 +02:00
const unify = document.getElementById("unify")
const textElement = document.getElementById("unify").getElementsByTagName("h4")[0]
unify.addEventListener("click", () => {
const oldHtml = textElement.innerHTML
textElement.innerHTML = "..."
browser.runtime.sendMessage({ function: "unify" }, response => {
if (response && response.response) textElement.innerHTML = oldHtml
})
})
} else {
2022-09-23 21:57:25 +02:00
document.getElementById("unify_div").style.display = "none"
}
2022-09-23 21:57:25 +02:00
} else {
currentSiteIsFrontend.classList.add("hide")
document.getElementById("unify_div").style.display = "none"
}
2022-09-23 21:57:25 +02:00
})
2022-02-02 19:22:34 +01:00
})
2022-02-01 19:37:43 +01:00
2022-09-23 21:57:25 +02:00
for (const service in config.services) {
divs[service].toggle.all.addEventListener("change", () => {
2022-09-27 21:59:25 +02:00
browser.storage.local.get("options", r => {
let options = r.options
options[service].enabled = divs[service].toggle.all.checked
browser.storage.local.set({ options })
2022-09-23 21:57:25 +02:00
})
})
divs[service].toggle.current.addEventListener("change", () => {
2022-09-27 21:59:25 +02:00
browser.storage.local.get("options", r => {
let options = r.options
options[service].enabled = divs[service].toggle.current.checked
browser.storage.local.set({ options })
2022-09-23 21:57:25 +02:00
})
})
}
for (const a of document.getElementsByTagName("a")) {
a.addEventListener("click", e => {
if (!a.classList.contains("prevent")) {
browser.tabs.create({ url: a.getAttribute("href") })
e.preventDefault()
}
})
}