aboutsummaryrefslogtreecommitdiffstats
path: root/src/assets
diff options
context:
space:
mode:
authorManeraKai <manerakai@protonmail.com>2023-01-21 14:29:10 +0300
committerManeraKai <manerakai@protonmail.com>2023-01-21 14:29:10 +0300
commit57e32c8d7ac8a9fb34a6c5d76b677a9df7dc714f (patch)
treebbde34f9224c9944c26f9cb67575a32d71e773f9 /src/assets
parentUX/UI tweaks (diff)
downloadlibredirect-57e32c8d7ac8a9fb34a6c5d76b677a9df7dc714f.zip
Cleaned code. Fixed settings conversion not working
Diffstat (limited to 'src/assets')
-rw-r--r--src/assets/javascripts/general.js31
-rw-r--r--src/assets/javascripts/services.js290
-rw-r--r--src/assets/javascripts/utils.js66
3 files changed, 191 insertions, 196 deletions
diff --git a/src/assets/javascripts/general.js b/src/assets/javascripts/general.js
index b2ad7c25..0295eb07 100644
--- a/src/assets/javascripts/general.js
+++ b/src/assets/javascripts/general.js
@@ -1,4 +1,7 @@
"use strict"
+
+import utils from "./utils.js"
+
window.browser = window.browser || window.chrome
let exceptions
@@ -10,36 +13,16 @@ function isException(url) {
}
function init() {
- return new Promise(resolve => {
- browser.storage.local.get("options", r => {
- if (r.options) exceptions = r.options.exceptions
- resolve()
- })
+ return new Promise(async resolve => {
+ const options = await utils.getOptions()
+ if (options) exceptions = options.exceptions
+ resolve()
})
}
init()
browser.storage.onChanged.addListener(init)
-async function initDefaults() {
- return new Promise(resolve =>
- browser.storage.local.set(
- {
- options: {
- exceptions: {
- url: [],
- regex: [],
- },
- theme: "detect",
- popupServices: ["youtube", "twitter", "tiktok", "imgur", "reddit", "quora", "translate", "maps"],
- },
- },
- () => resolve()
- )
- )
-}
-
export default {
isException,
- initDefaults,
}
diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js
index 159e39e2..181dec00 100644
--- a/src/assets/javascripts/services.js
+++ b/src/assets/javascripts/services.js
@@ -1,20 +1,14 @@
-window.browser = window.browser || window.chrome
-
import utils from "./utils.js"
+window.browser = window.browser || window.chrome
+
let config, options
function init() {
return new Promise(async resolve => {
- browser.storage.local.get(["options"], r => {
- options = r.options
- fetch("/config.json")
- .then(response => response.text())
- .then(configData => {
- config = JSON.parse(configData)
- resolve()
- })
- })
+ options = await utils.getOptions()
+ config = await utils.getConfig()
+ resolve()
})
}
@@ -29,8 +23,8 @@ function all(service, frontend, options, config) {
instances.push(...options[frontend])
}
}
- } else {
- instances.push(...options[frontend])
+ } else if (options[frontend]) {
+ instances = options[frontend]
}
return instances
}
@@ -393,34 +387,28 @@ function redirect(url, type, initiator, forceRedirection, tabId) {
}
function computeService(url, returnFrontend) {
- return new Promise(resolve => {
- fetch("/config.json")
- .then(response => response.text())
- .then(configData => {
- const config = JSON.parse(configData)
- browser.storage.local.get(["redirects", "options"], r => {
- const options = r.options
- for (const service in config.services) {
- if (regexArray(service, url, config)) {
- resolve(service)
- return
- } else {
- for (const frontend in config.services[service].frontends) {
- if (all(service, frontend, options, config).includes(utils.protocolHost(url))) {
- if (returnFrontend) resolve([service, frontend, utils.protocolHost(url)])
- else resolve(service)
- return
- }
- }
- }
+ return new Promise(async resolve => {
+ const config = await utils.getConfig()
+ const options = await utils.getOptions()
+ for (const service in config.services) {
+ if (regexArray(service, url, config)) {
+ resolve(service)
+ return
+ } else {
+ for (const frontend in config.services[service].frontends) {
+ if (all(service, frontend, options, config).includes(utils.protocolHost(url))) {
+ if (returnFrontend) resolve([service, frontend, utils.protocolHost(url)])
+ else resolve(service)
+ return
}
- resolve()
- })
- })
+ }
+ }
+ }
+ resolve()
})
}
-function switchInstance(url) {
+function _switchInstance(url) {
return new Promise(async resolve => {
await init()
const protocolHost = utils.protocolHost(url)
@@ -499,102 +487,109 @@ function reverse(url, urlString) {
function initDefaults() {
return new Promise(resolve => {
- fetch("/config.json")
- .then(response => response.text())
- .then(configData => {
- browser.storage.local.get(["options"], r => {
- let options = r.options
- let config = JSON.parse(configData)
- for (const service in config.services) {
- options[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] = []
- }
- }
+ browser.storage.local.clear(async () => {
+ let config = await utils.getConfig()
+ let options = {}
+ for (const service in config.services) {
+ options[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] = []
}
- browser.storage.local.set(
- { options },
- () => resolve()
- )
- })
- })
- })
-}
-
-function backupOptions() {
- return new Promise(resolve => {
- browser.storage.local.get(
- "options", r => {
- const oldOptions = r.options
- browser.storage.local.clear(() => {
- browser.storage.local.set({ oldOptions },
- () => resolve()
- )
- })
+ }
+ }
+ options['exceptions'] = {
+ url: [],
+ regex: [],
+ }
+ options['theme'] = "detect"
+ options['popupServices'] = ["youtube", "twitter", "tiktok", "imgur", "reddit", "quora", "translate", "maps"]
- })
+ browser.storage.local.set({ options },
+ () => resolve()
+ )
+ })
})
}
function upgradeOptions() {
- return new Promise(resolve => {
- fetch("/config.json")
- .then(response => response.text())
- .then(configData => {
- browser.storage.local.get(["oldOptions", "options"], r => {
- const oldOptions = r.oldOptions
- let options = r.options
- const config = JSON.parse(configData)
+ return new Promise(async resolve => {
+ const oldOptions = await utils.getOptions()
+ const config = await utils.getConfig()
- options.exceptions = oldOptions.exceptions
- options.theme = oldOptions.theme
- options.popupServices = oldOptions.popupServices
+ let options = {}
- for (const service in config.services) {
- options[service] = oldOptions[service]
- options[service].remove("embedFrontend")
+ options.exceptions = oldOptions.exceptions
+ options.theme = oldOptions.theme
+ options.popupServices = oldOptions.popupServices
- for (const frontend in network.services[service].frontends) {
- options[frontend] = [
- ...oldOptions[frontend].clearnet.enabled,
- ...oldOptions[frontend].clearnet.custom
- ]
- }
+ for (const service in config.services) {
+ if (service in oldOptions) {
+ options[service] = oldOptions[service]
+ delete options[service].embedFrontend
+ }
+ else {
+ options[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] = []
}
- browser.storage.local.set({ options }, () => {
- browser.storage.local.remove("oldOptions", () => {
- resolve()
- })
- })
- })
+ }
+ }
+
+ for (const frontend in config.services[service].frontends) {
+ if (config.services[service].frontends[frontend].instanceList) {
+ if (frontend in oldOptions) {
+ options[frontend] = [
+ ...oldOptions[frontend].clearnet.enabled,
+ ...oldOptions[frontend].clearnet.custom
+ ]
+ }
+ else {
+ options[frontend] = []
+ }
+ }
+ }
+ }
+
+ browser.storage.local.clear(() => {
+ browser.storage.local.set({ options }, () => {
+ resolve()
})
+ })
})
}
function processUpdate() {
- return new Promise(resolve => {
- fetch("/config.json")
- .then(response => response.text())
- .then(configJson => {
- let config = JSON.parse(configJson)
- browser.storage.local.get(["options"], async r => {
- let options = r.options
- for (const service in config.services) {
- if (!options[service]) options[service] = {}
- for (const defaultOption in config.services[service].options) {
- if (options[service][defaultOption] === undefined) {
- options[service][defaultOption] = config.services[service].options[defaultOption]
- }
- }
- }
- browser.storage.local.set({ options })
- resolve()
- })
- })
+ return new Promise(async resolve => {
+ let config = await utils.getConfig()
+ let options = await utils.getOptions()
+ for (const service in config.services) {
+ if (!options[service]) options[service] = {}
+ for (const defaultOption in config.services[service].options) {
+ if (options[service][defaultOption] === undefined) {
+ options[service][defaultOption] = config.services[service].options[defaultOption]
+ }
+ }
+
+ for (const frontend in config.services[service].frontends) {
+ if (options[frontend] === undefined && config.services[service].frontends[frontend].instanceList) {
+ options[frontend] = []
+ }
+ else if (frontend in options && frontend in !config.services[service].frontends) {
+ delete options[frontend]
+ }
+ }
+ }
+ browser.storage.local.set({ options }, () => {
+ resolve()
+ })
})
}
@@ -636,14 +631,69 @@ function modifyContentSecurityPolicy(details) {
}
}
+function copyRaw(test, copyRawElement) {
+ return new Promise(resolve => {
+ browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
+ let currTab = tabs[0]
+ if (currTab) {
+ let url
+ try {
+ url = new URL(currTab.url)
+ } catch {
+ resolve()
+ return
+ }
+
+ const newUrl = await reverse(url)
+
+ if (newUrl) {
+ resolve(newUrl)
+ if (test) return
+ navigator.clipboard.writeText(newUrl)
+ if (copyRawElement) {
+ const textElement = copyRawElement.getElementsByTagName("h4")[0]
+ const oldHtml = textElement.innerHTML
+ textElement.innerHTML = browser.i18n.getMessage("copied")
+ setTimeout(() => (textElement.innerHTML = oldHtml), 1000)
+ }
+ }
+ }
+ resolve()
+ })
+ })
+}
+
+function switchInstance(test) {
+ return new Promise(resolve => {
+ browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
+ let currTab = tabs[0]
+ if (currTab) {
+ let url
+ try {
+ url = new URL(currTab.url)
+ } catch {
+ resolve()
+ return
+ }
+ const newUrl = await _switchInstance(url)
+
+ if (newUrl) {
+ if (!test) browser.tabs.update({ url: newUrl })
+ resolve(true)
+ } else resolve()
+ }
+ })
+ })
+}
+
export default {
redirect,
computeService,
- switchInstance,
reverse,
initDefaults,
upgradeOptions,
- backupOptions,
processUpdate,
modifyContentSecurityPolicy,
+ copyRaw,
+ switchInstance
}
diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js
index 3a7bd839..c8a771d9 100644
--- a/src/assets/javascripts/utils.js
+++ b/src/assets/javascripts/utils.js
@@ -1,7 +1,5 @@
window.browser = window.browser || window.chrome
-import servicesHelper from "./services.js"
-
function getRandomInstance(instances) {
return instances[~~(instances.length * Math.random())]
}
@@ -15,59 +13,23 @@ function protocolHost(url) {
return `${url.protocol}//${url.host}`
}
-function copyRaw(test, copyRawElement) {
+function getConfig() {
return new Promise(resolve => {
- browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
- let currTab = tabs[0]
- if (currTab) {
- let url
- try {
- url = new URL(currTab.url)
- } catch {
- resolve()
- return
- }
-
- const newUrl = await servicesHelper.reverse(url)
-
- if (newUrl) {
- resolve(newUrl)
- if (test) return
- navigator.clipboard.writeText(newUrl)
- if (copyRawElement) {
- const textElement = copyRawElement.getElementsByTagName("h4")[0]
- const oldHtml = textElement.innerHTML
- textElement.innerHTML = browser.i18n.getMessage("copied")
- setTimeout(() => (textElement.innerHTML = oldHtml), 1000)
- }
- }
- }
- resolve()
- })
+ fetch("/config.json")
+ .then(response => response.text())
+ .then(json => {
+ resolve(JSON.parse(json))
+ return
+ })
})
}
-function switchInstance(test) {
- return new Promise(resolve => {
- browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
- let currTab = tabs[0]
- if (currTab) {
- let url
- try {
- url = new URL(currTab.url)
- } catch {
- resolve()
- return
- }
- const newUrl = await servicesHelper.switchInstance(url)
-
- if (newUrl) {
- if (!test) browser.tabs.update({ url: newUrl })
- resolve(true)
- } else resolve()
- }
+function getOptions() {
+ return new Promise(resolve =>
+ browser.storage.local.get("options", r => {
+ resolve(r.options)
})
- })
+ )
}
function getBlacklist() {
@@ -102,9 +64,9 @@ function getList() {
export default {
getRandomInstance,
protocolHost,
- switchInstance,
- copyRaw,
getList,
getBlacklist,
camelCase,
+ getConfig,
+ getOptions
}