Unify Localstorage

This commit is contained in:
Hygna 2022-10-01 11:17:32 +01:00
parent 18facf37a2
commit e21ecb4e1d
No known key found for this signature in database
10 changed files with 119 additions and 83 deletions

View File

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor">
<path d="M19.35 10.04C18.67 6.59 15.64 4 12 4 9.11 4 6.6 5.64 5.35 8.04 2.34 8.36 0 10.91 0 14c0 3.31 2.69 6 6 6h13c2.76 0 5-2.24 5-5 0-2.64-2.05-4.78-4.65-4.96zM14 13v4h-4v-4H7l5-5 5 5h-3z"></path>
</svg>

After

Width:  |  Height:  |  Size: 316 B

View File

@ -0,0 +1,15 @@
window.browser = window.browser || window.chrome
browser.storage.local.get(["localstorage", "tmp"], r => {
let localstorageJson = r.localstorage
const frontend = r.tmp[0]
const items = r.tmp[1]
localstorageJson[frontend] = {}
for (const item of items) {
let tmp = localStorage.getItem(item)
if (tmp) localstorageJson[frontend][item] = tmp
}
browser.storage.local.set({ localstorage: localstorageJson })
})

View File

@ -0,0 +1,60 @@
async function getConfig() {
return new Promise(resolve => {
fetch("/config/config.json")
.then(response => response.text())
.then(data => {
config = JSON.parse(data)
resolve()
})
})
}
let config
await getConfig()
async function initDefaults() {
return new Promise(async resolve => {
fetch("/instances/data.json")
.then(response => response.text())
.then(async data => {
browser.storage.local.get(["options", "blacklists"], async r => {
let redirects = JSON.parse(data)
let options = r.options
let targets = {}
const localstorage = {}
const latency = {}
for (const service in config.services) {
options[service] = {}
if (config.services[service].targets == "datajson") {
targets[service] = redirects[service]
}
for (const defaultOption in config.services[service].options) {
options[service][defaultOption] = config.services[service].options[defaultOption]
}
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) {
options[frontend] = {}
for (const network in config.networks) {
options[frontend][network] = {}
options[frontend][network].enabled = JSON.parse(data)[frontend][network]
options[frontend][network].custom = []
}
for (const blacklist in r.blacklists) {
for (const instance of r.blacklists[blacklist]) {
let i = options[frontend].clearnet.enabled.indexOf(instance)
if (i > -1) options[frontend].clearnet.enabled.splice(i, 1)
}
}
}
}
}
browser.storage.local.set({ redirects, options, targets, latency, localstorage })
resolve()
})
})
})
}
export default {
initDefaults,
}

View File

@ -2,7 +2,7 @@ window.browser = window.browser || window.chrome
import utils from "./utils.js"
let config, redirects, options, targets, blacklists
let config, redirects, options, blacklists
async function getConfig() {
return new Promise(resolve => {
@ -17,12 +17,10 @@ async function getConfig() {
function init() {
return new Promise(async resolve => {
// await getConfig()
browser.storage.local.get(["options", "targets", "redirects", "blacklists"], r => {
browser.storage.local.get(["options", "redirects", "blacklists"], r => {
if (r.options) {
blacklists = r.blacklists
redirects = r.redirects
targets = r.targets
options = r.options
}
resolve()
@ -37,7 +35,6 @@ function fetchFrontendInstanceList(service, frontend) {
let tmp = []
if (config.services[service].frontends[frontend].instanceList) {
for (const network in config.networks) {
if (!redirects[frontend]) console.log(frontend)
tmp.push(...redirects[frontend][network], ...options[frontend][network].custom)
}
} else if (config.services[service].frontends[frontend].singleInstance) tmp = config.services[service].frontends[frontend].singleInstance
@ -45,8 +42,6 @@ function fetchFrontendInstanceList(service, frontend) {
}
function all(service, frontend) {
// init()
// getConfig()
let instances = []
if (!frontend) {
for (const frontend in config.services[service].frontends) {
@ -90,10 +85,9 @@ function redirect(url, type, initiator) {
if (!regexArray(service, url)) continue
if (Object.keys(config.services[service].frontends).length > 1) {
frontend = options[service].frontend
} else {
frontend = Object.keys(config.services[service].frontends)[0]
}
if (type == "sub_frame") frontend = options[service].embedFrontend
else frontend = options[service].frontend
} else frontend = Object.keys(config.services[service].frontends)[0]
if (config.services[service].frontends[frontend].instanceList) {
let instanceList = [...options[frontend][network].enabled, ...options[frontend][network].custom]
@ -103,7 +97,7 @@ function redirect(url, type, initiator) {
} else if (config.services[service].frontends[frontend].singleInstance) randomInstance = config.services[service].frontends[frontend].singleInstance
break
}
if (frontend == null) return
if (!frontend) return
// Here is a (temperory) space for defining constants required in 2 or more switch cases.
// When possible, try have the two switch cases share all their code as done with searx and searxng.
@ -391,49 +385,6 @@ function redirect(url, type, initiator) {
}
}
async function initDefaults() {
return new Promise(async resolve => {
fetch("/instances/data.json")
.then(response => response.text())
.then(async data => {
browser.storage.local.get(["options", "blacklists"], async r => {
let redirects = JSON.parse(data)
let options = r.options
let targets = {}
const localstorage = {}
const latency = {}
for (const service in config.services) {
options[service] = {}
if (config.services[service].targets == "datajson") {
targets[service] = redirects[service]
}
for (const defaultOption in config.services[service].options) {
options[service][defaultOption] = config.services[service].options[defaultOption]
}
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) {
options[frontend] = {}
for (const network in config.networks) {
options[frontend][network] = {}
options[frontend][network].enabled = JSON.parse(data)[frontend][network]
options[frontend][network].custom = []
}
for (const blacklist in r.blacklists) {
for (const instance of r.blacklists[blacklist]) {
let i = options[frontend].clearnet.enabled.indexOf(instance)
if (i > -1) options[frontend].clearnet.enabled.splice(i, 1)
}
}
}
}
}
browser.storage.local.set({ redirects, options, targets, latency, localstorage })
resolve()
})
})
})
}
function computeService(url, returnFrontend) {
for (const service in config.services) {
if (regexArray(service, url)) {
@ -454,8 +405,6 @@ function computeService(url, returnFrontend) {
function switchInstance(url) {
return new Promise(async resolve => {
// await init()
// await getConfig()
const protocolHost = utils.protocolHost(url)
for (const service in config.services) {
if (!options[service].enabled) continue
@ -487,8 +436,6 @@ function switchInstance(url) {
function reverse(url) {
return new Promise(async resolve => {
// await init()
// await getConfig()
let protocolHost = utils.protocolHost(url)
let currentService
for (const service in config.services) {
@ -517,8 +464,6 @@ function reverse(url) {
function unifyPreferences(url, tabId) {
return new Promise(async resolve => {
// await init()
// await getConfig()
const protocolHost = utils.protocolHost(url)
let currentFrontend, currentService
serviceloop: for (const service in config.services) {
@ -540,18 +485,14 @@ function unifyPreferences(url, tabId) {
}
}
if ("localstorage" in frontend.preferences) {
browser.storage.local.set({ tmp: [currentFrontend, frontend.preferences.localstorage] })
browser.tabs.executeScript(tabId, {
code: "const frontend = " + frontend,
code: "const items = " + config.services[currentService].frontends[currentFrontend].preferences.localStorage,
//file: "/assets/javascripts/get-localstorage.js",
file: "/assets/javascripts/get-localstorage.js",
runAt: "document_start",
})
for (const instance of instancesList)
browser.tabs.create({ url: instance }, tab =>
browser.tabs.executeScript(tab.id, {
code: "const frontend = " + frontend,
code: "const items = " + config.services[currentService].frontends[currentFrontend].preferences.localStorage,
file: "/assets/javascripts/set-localstorage.js",
runAt: "document_start",
})
@ -594,7 +535,6 @@ function setRedirects(redirects) {
export default {
redirect,
initDefaults,
computeService,
switchInstance,
reverse,

View File

@ -0,0 +1,13 @@
window.browser = window.browser || window.chrome
browser.storage.local.get(["localstorage", "tmp"], r => {
const localstorageJson = r.localstorage
const frontend = r.tmp[0]
const items = localstorageJson[frontend]
for (const item in items) {
localStorage.setItem(item, items[item])
}
window.close()
})

View File

@ -244,22 +244,20 @@ async function testLatency(element, instances, frontend) {
return new Promise(async resolve => {
let myList = {}
let latencyThreshold, options
//let redirectsChecks = []
browser.storage.local.get(["options"], r => {
latencyThreshold = r.options.latencyThreshold
//redirectsChecks = r.options[frontend].clearnet.enabled
options = r.options
})
for (const href of instances)
for (const href of instances) {
await ping(href).then(time => {
let color
if (time) {
myList[href] = time
let color
if (time <= 1000) color = "green"
else if (time <= 2000) color = "orange"
else color = "red"
if (time > latencyThreshold) {
if (time > latencyThreshold && options[frontend].clearnet.enabled.includes(href)) {
options[frontend].clearnet.enabled.splice(options[frontend].clearnet.enabled.indexOf(href), 1)
}
@ -268,8 +266,13 @@ async function testLatency(element, instances, frontend) {
else if (time > 5000) text = `ERROR: ${time - 5000}`
else text = `${time}ms`
element.innerHTML = `${href}:&nbsp;<span style="color:${color};">${text}</span>`
} else {
color = "red"
element.innerHTML = `${href}:&nbsp;<span style="color:${color};">Server not found</span>`
if (options[frontend].clearnet.enabled.includes(href)) options[frontend].clearnet.enabled.splice(options[frontend].clearnet.enabled.indexOf(href), 1)
}
})
}
browser.storage.local.set({ options })
resolve(myList)
})
@ -423,6 +426,7 @@ function latency(service, frontend, document, location) {
latencyElement.addEventListener("click", reloadWindow)
browser.storage.local.get(["redirects", "latency"], r => {
let redirects = r.redirects
let latency = r.latency
const oldHtml = latencyLabel.innerHTML
latencyLabel.innerHTML = "..."
testLatency(latencyLabel, redirects[frontend].clearnet, frontend).then(r => {

View File

@ -3,6 +3,7 @@
import generalHelper from "../../assets/javascripts/general.js"
import utils from "../../assets/javascripts/utils.js"
import servicesHelper from "../../assets/javascripts/services.js"
import initHelper from "../../assets/javascripts/init.js"
window.browser = window.browser || window.chrome
@ -38,7 +39,7 @@ function initDefaults() {
.then(async data => {
browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => {
await generalHelper.initDefaults()
await servicesHelper.initDefaults()
await initHelper.initDefaults()
})
})
})

View File

@ -3252,19 +3252,19 @@
<div class="about">
<div class="some-block option-block">
<h4>Donate: ♥️</h4>
<h4><a href='https://libredirect.github.io/donate'>https://libredirect.github.io/donate</a> </h4>
<h4><a href='https://libredirect.codeberg.page/donate'>https://libredirect.codeberg.page/donate</a> </h4>
</div>
<div class="some-block option-block">
<h4>FAQ:</h4>
<h4><a href='https://libredirect.github.io/faq'>https://libredirect.github.io/faq</a></h4>
<h4><a href='https://libredirect.codeberg.page/faq'>https://libredirect.codeberg.page/faq</a></h4>
</div>
<div class="some-block option-block">
<h4>Docs:</h4>
<h4><a href='https://libredirect.github.io/docs'>https://libredirect.github.io/docs</a></h4>
<h4><a href='https://libredirect.codeberg.page/docs'>https://libredirect.codeberg.page/docs</a></h4>
</div>
<div class="some-block option-block">
<h4>Source Code:</h4>
<h4><a href='https://libredirect.github.io/source_code'>https://libredirect.github.io/source_code</a></h4>
<h4><a href='https://libredirect.codeberg.page/source_code'>https://libredirect.codeberg.page/source_code</a></h4>
</div>
</div>
</section>

View File

@ -6,19 +6,19 @@
<div class="about">
<div class="some-block option-block">
<h4>Donate: ♥️</h4>
<h4><a href='https://libredirect.github.io/donate'>https://libredirect.github.io/donate</a> </h4>
<h4><a href='https://libredirect.codeberg.page/donate'>https://libredirect.codeberg.page/donate</a> </h4>
</div>
<div class="some-block option-block">
<h4>FAQ:</h4>
<h4><a href='https://libredirect.github.io/faq'>https://libredirect.github.io/faq</a></h4>
<h4><a href='https://libredirect.codeberg.page/faq'>https://libredirect.codeberg.page/faq</a></h4>
</div>
<div class="some-block option-block">
<h4>Docs:</h4>
<h4><a href='https://libredirect.github.io/docs'>https://libredirect.github.io/docs</a></h4>
<h4><a href='https://libredirect.codeberg.page/docs'>https://libredirect.codeberg.page/docs</a></h4>
</div>
<div class="some-block option-block">
<h4>Source Code:</h4>
<h4><a href='https://libredirect.github.io/source_code'>https://libredirect.github.io/source_code</a></h4>
<h4><a href='https://libredirect.codeberg.page/source_code'>https://libredirect.codeberg.page/source_code</a></h4>
</div>
</div>
</section>

View File

@ -40,7 +40,7 @@ function changeFrontendsSettings(service) {
}
if (config.services[service].embeddable) {
if (!config.services[service].frontends[divs[service].frontend.value].instanceList) {
if (!config.services[service].frontends[divs[service].frontend.value].embeddable) {
divs[service].embedFrontend.disabled = false
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].embeddable) {