Cleaned code. Fixed settings conversion not working
This commit is contained in:
parent
6f338f2c66
commit
57e32c8d7a
12
README.md
12
README.md
@ -67,26 +67,18 @@ npm update
|
||||
npm install
|
||||
```
|
||||
|
||||
To generate HTML that uses `config.json` (needed to develop/build the extension), run:
|
||||
Generate the HTML pages:
|
||||
|
||||
```
|
||||
npm run ejs
|
||||
npm run pug
|
||||
```
|
||||
|
||||
Afterwards, you will need to run it if you modify `config.json` or any files ending with .ejs.
|
||||
|
||||
### Build the extension zip archive:
|
||||
|
||||
```
|
||||
npm run build
|
||||
```
|
||||
|
||||
### Run automated tests
|
||||
|
||||
```
|
||||
npm run test
|
||||
```
|
||||
|
||||
### Test in Firefox
|
||||
|
||||
```
|
||||
|
@ -9,7 +9,6 @@
|
||||
"start": "web-ext run",
|
||||
"build": "web-ext build",
|
||||
"test": "web-ext lint",
|
||||
"instances": "python3 src/instances/get_instances.py && git update-index --assume-unchanged src/instances/blacklist.json src/instances/data.json",
|
||||
"pug": "pug --pretty --basedir ./ --obj ./src/config.json src/pages/options/index.pug --out src/pages/options/ && pug --pretty --basedir ./ --obj ./src/config.json src/pages/popup/popup.pug --out src/pages/popup/"
|
||||
},
|
||||
"repository": {
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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()
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
options['exceptions'] = {
|
||||
url: [],
|
||||
regex: [],
|
||||
}
|
||||
options['theme'] = "detect"
|
||||
options['popupServices'] = ["youtube", "twitter", "tiktok", "imgur", "reddit", "quora", "translate", "maps"]
|
||||
|
||||
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()
|
||||
)
|
||||
})
|
||||
|
||||
})
|
||||
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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "__MSG_extensionName__",
|
||||
"description": "__MSG_extensionDescription__",
|
||||
"version": "2.3.4",
|
||||
"version": "3.0.0",
|
||||
"manifest_version": 2,
|
||||
"browser_specific_settings": {
|
||||
"gecko": {
|
||||
|
@ -9,33 +9,17 @@ window.browser = window.browser || window.chrome
|
||||
browser.runtime.onInstalled.addListener(async details => {
|
||||
if (details.previousVersion != browser.runtime.getManifest().version) {
|
||||
// ^Used to prevent this running when debugging with auto-reload
|
||||
browser.runtime.openOptionsPage()
|
||||
switch (details.reason) {
|
||||
case "install":
|
||||
browser.storage.local.get("options", async r => {
|
||||
if (!r.options) {
|
||||
await generalHelper.initDefaults()
|
||||
await servicesHelper.initDefaults()
|
||||
}
|
||||
})
|
||||
break
|
||||
case "update":
|
||||
switch (details.previousVersion) {
|
||||
case "2.3.4":
|
||||
browser.storage.local.get("options", async r => {
|
||||
if (!r.options) {
|
||||
await servicesHelper.backupOptions()
|
||||
await generalHelper.initDefaults()
|
||||
await servicesHelper.initDefaults()
|
||||
await servicesHelper.upgradeOptions()
|
||||
}
|
||||
})
|
||||
break
|
||||
default:
|
||||
await servicesHelper.processUpdate()
|
||||
}
|
||||
if (details.reason == "install") {
|
||||
if (!(await utils.getOptions())) {
|
||||
await servicesHelper.initDefaults()
|
||||
}
|
||||
}
|
||||
else if (details.reason == "update") {
|
||||
await servicesHelper.upgradeOptions()
|
||||
// await servicesHelper.processUpdate()
|
||||
}
|
||||
}
|
||||
browser.runtime.openOptionsPage()
|
||||
})
|
||||
|
||||
let tabIdRedirects = {}
|
||||
@ -86,8 +70,8 @@ browser.tabs.onRemoved.addListener(tabId => {
|
||||
})
|
||||
|
||||
browser.commands.onCommand.addListener(command => {
|
||||
if (command === "switchInstance") utils.switchInstance()
|
||||
else if (command == "copyRaw") utils.copyRaw()
|
||||
if (command === "switchInstance") servicesHelper.switchInstance()
|
||||
else if (command == "copyRaw") servicesHelper.copyRaw()
|
||||
})
|
||||
|
||||
browser.contextMenus.create({
|
||||
@ -134,7 +118,7 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
|
||||
return new Promise(async resolve => {
|
||||
switch (info.menuItemId) {
|
||||
case "switchInstance":
|
||||
utils.switchInstance()
|
||||
servicesHelper.switchInstance()
|
||||
resolve()
|
||||
return
|
||||
case "settings":
|
||||
@ -142,7 +126,7 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
|
||||
resolve()
|
||||
return
|
||||
case "copyRaw":
|
||||
utils.copyRaw()
|
||||
servicesHelper.copyRaw()
|
||||
resolve()
|
||||
return
|
||||
case "toggleTab":
|
||||
@ -155,13 +139,11 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
|
||||
const url = new URL(tab.url)
|
||||
const service = await servicesHelper.computeService(url)
|
||||
if (service) {
|
||||
browser.storage.local.get("options", async r => {
|
||||
if (r.options[service].enabled) tabIdRedirects[tab.id] = false
|
||||
else tabIdRedirects[tab.id] = true
|
||||
await handleToggleTab(tab)
|
||||
resolve()
|
||||
return
|
||||
})
|
||||
if ((await utils.getOptions())[service].enabled) tabIdRedirects[tab.id] = false
|
||||
else tabIdRedirects[tab.id] = true
|
||||
await handleToggleTab(tab)
|
||||
resolve()
|
||||
return
|
||||
} else {
|
||||
tabIdRedirects[tab.id] = false
|
||||
await handleToggleTab(tab)
|
||||
|
@ -13,19 +13,9 @@ for (const a of document.getElementById("links").getElementsByTagName("a")) {
|
||||
})
|
||||
}
|
||||
|
||||
await new Promise(resolve => {
|
||||
fetch("/config.json").then(response => response.text())
|
||||
.then(data => {
|
||||
config = JSON.parse(data)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
await new Promise(resolve => {
|
||||
browser.storage.local.get("options", r => {
|
||||
options = r.options
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
|
||||
config = await utils.getConfig()
|
||||
options = await utils.getOptions()
|
||||
|
||||
function changeFrontendsSettings(service) {
|
||||
for (const frontend in config.services[service].frontends) {
|
||||
@ -46,12 +36,15 @@ function loadPage(path) {
|
||||
for (const section of document.getElementById("pages").getElementsByTagName("section")) section.style.display = "none"
|
||||
document.getElementById(`${path}_page`).style.display = "block"
|
||||
|
||||
for (const a of document.getElementById("links").getElementsByTagName("a"))
|
||||
if (a.getAttribute("href") == `#${path}`) a.classList.add("selected")
|
||||
else a.classList.remove("selected")
|
||||
for (const a of document.getElementById("links").getElementsByTagName("a")) {
|
||||
if (a.getAttribute("href") == `#${path}`) {
|
||||
a.classList.add("selected")
|
||||
} else {
|
||||
a.classList.remove("selected")
|
||||
}
|
||||
}
|
||||
|
||||
let stateObj = { id: "100" }
|
||||
window.history.pushState(stateObj, "Page 2", `/pages/options/index.html#${path}`)
|
||||
window.history.pushState({ id: "100" }, "Page 2", `/pages/options/index.html#${path}`)
|
||||
|
||||
if (path != 'general' && path != 'about') {
|
||||
const service = path;
|
||||
@ -62,16 +55,14 @@ function loadPage(path) {
|
||||
if (typeof config.services[service].options[option] == "boolean") divs[service][option].checked = options[service][option]
|
||||
else divs[service][option].value = options[service][option]
|
||||
|
||||
divs[service][option].addEventListener("change", () => {
|
||||
browser.storage.local.get("options", r => {
|
||||
let options = r.options
|
||||
if (typeof config.services[service].options[option] == "boolean")
|
||||
options[service][option] = divs[service][option].checked
|
||||
else
|
||||
options[service][option] = divs[service][option].value
|
||||
browser.storage.local.set({ options })
|
||||
changeFrontendsSettings(service)
|
||||
})
|
||||
divs[service][option].addEventListener("change", async () => {
|
||||
let options = await utils.getOptions()
|
||||
if (typeof config.services[service].options[option] == "boolean")
|
||||
options[service][option] = divs[service][option].checked
|
||||
else
|
||||
options[service][option] = divs[service][option].value
|
||||
browser.storage.local.set({ options })
|
||||
changeFrontendsSettings(service)
|
||||
})
|
||||
}
|
||||
|
||||
@ -88,7 +79,7 @@ function loadPage(path) {
|
||||
|
||||
for (const frontend in config.services[service].frontends) {
|
||||
if (config.services[service].frontends[frontend].instanceList) {
|
||||
processDefaultCustomInstances(frontend, document)
|
||||
processCustomInstances(frontend, document)
|
||||
}
|
||||
}
|
||||
|
||||
@ -104,18 +95,9 @@ function loadPage(path) {
|
||||
}
|
||||
}
|
||||
|
||||
async function processDefaultCustomInstances(frontend, document) {
|
||||
let customInstances = []
|
||||
let options
|
||||
await new Promise(async resolve =>
|
||||
browser.storage.local.get(["options"], r => {
|
||||
customInstances = r.options[frontend]
|
||||
options = r.options
|
||||
resolve()
|
||||
})
|
||||
)
|
||||
|
||||
localise.localisePage()
|
||||
async function processCustomInstances(frontend, document) {
|
||||
let options = await utils.getOptions()
|
||||
let customInstances = options[frontend]
|
||||
|
||||
function calcCustomInstances() {
|
||||
document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].innerHTML = customInstances
|
||||
@ -133,10 +115,12 @@ async function processDefaultCustomInstances(frontend, document) {
|
||||
)
|
||||
.join("\n")
|
||||
|
||||
customInstances = options[frontend]
|
||||
for (const item of customInstances) {
|
||||
document.getElementById(frontend).getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => {
|
||||
let index = customInstances.indexOf(item)
|
||||
if (index > -1) customInstances.splice(index, 1)
|
||||
options = await utils.getOptions()
|
||||
options[frontend] = customInstances
|
||||
browser.storage.local.set({ options }, () => calcCustomInstances())
|
||||
})
|
||||
@ -144,7 +128,6 @@ async function processDefaultCustomInstances(frontend, document) {
|
||||
}
|
||||
calcCustomInstances()
|
||||
document.getElementById(frontend).getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => {
|
||||
event.preventDefault();
|
||||
event.preventDefault()
|
||||
let frontendCustomInstanceInput = document.getElementById(frontend).getElementsByClassName("custom-instance")[0]
|
||||
let url
|
||||
@ -157,6 +140,7 @@ async function processDefaultCustomInstances(frontend, document) {
|
||||
if (frontendCustomInstanceInput.validity.valid) {
|
||||
if (!customInstances.includes(protocolHostVar)) {
|
||||
customInstances.push(protocolHostVar)
|
||||
options = await utils.getOptions()
|
||||
options[frontend] = customInstances
|
||||
browser.storage.local.set({ options }, () => {
|
||||
frontendCustomInstanceInput.value = ""
|
||||
@ -172,32 +156,32 @@ function createList(frontend, networks, document, redirects, blacklist) {
|
||||
if (redirects[frontend]) {
|
||||
if (redirects[frontend][network].length > 0) {
|
||||
document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0].innerHTML = [
|
||||
`
|
||||
<div class="some-block option-block">
|
||||
<h4>${utils.camelCase(network)}</h4>
|
||||
</div>
|
||||
`,
|
||||
`<div class="some-block option-block">
|
||||
<h4>${utils.camelCase(network)}</h4>
|
||||
</div>`,
|
||||
...redirects[frontend][network]
|
||||
.sort((a, b) =>
|
||||
(blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b))
|
||||
)
|
||||
.map(x => {
|
||||
const cloudflare = blacklist.cloudflare.includes(x) ? ' <a target="_blank" href="https://libredirect.github.io/docs.html#instances"><span style="color:red;">cloudflare</span></a>' : ""
|
||||
const cloudflare = blacklist.cloudflare.includes(x) ?
|
||||
` <a target="_blank" href="https://libredirect.github.io/docs.html#instances">
|
||||
<span style="color:red;">cloudflare</span>
|
||||
</a>` : ""
|
||||
|
||||
let warnings = [cloudflare].join(" ")
|
||||
return `
|
||||
<div>
|
||||
<x>
|
||||
<a href="${x}" target="_blank">${x}</a>${warnings}
|
||||
</x>
|
||||
</div>`
|
||||
const warnings = [cloudflare].join(" ")
|
||||
return `<div>
|
||||
<x>
|
||||
<a href="${x}" target="_blank">${x}</a>${warnings}
|
||||
</x>
|
||||
</div>`
|
||||
}),
|
||||
'<br>'
|
||||
].join("\n<hr>\n")
|
||||
}
|
||||
} else {
|
||||
document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0].innerHTML =
|
||||
`<div class="some-block option-block">No instances found...</div>`
|
||||
`<div class="some-block option-block">No instances found.</div>`
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -1,42 +1,41 @@
|
||||
window.browser = window.browser || window.chrome
|
||||
|
||||
import localise from "../../assets/javascripts/localise.js"
|
||||
import utils from "../../assets/javascripts/utils.js"
|
||||
|
||||
function changeTheme() {
|
||||
return new Promise(resolve => {
|
||||
browser.storage.local.get("options", r => {
|
||||
switch (r.options.theme) {
|
||||
case "dark":
|
||||
document.body.classList.add("dark-theme")
|
||||
document.body.classList.remove("light-theme")
|
||||
for (const element of document.body.getElementsByClassName('dark')) {
|
||||
element.style.display = 'none';
|
||||
}
|
||||
break
|
||||
case "light":
|
||||
return new Promise(async resolve => {
|
||||
switch ((await utils.getOptions()).theme) {
|
||||
case "dark":
|
||||
document.body.classList.add("dark-theme")
|
||||
document.body.classList.remove("light-theme")
|
||||
for (const element of document.body.getElementsByClassName('dark')) {
|
||||
element.style.display = 'none';
|
||||
}
|
||||
break
|
||||
case "light":
|
||||
document.body.classList.add("light-theme")
|
||||
document.body.classList.remove("dark-theme")
|
||||
for (const element of document.body.getElementsByClassName('light')) {
|
||||
element.style.display = 'none';
|
||||
}
|
||||
break
|
||||
default:
|
||||
if (matchMedia("(prefers-color-scheme: light)").matches) {
|
||||
document.body.classList.add("light-theme")
|
||||
document.body.classList.remove("dark-theme")
|
||||
for (const element of document.body.getElementsByClassName('light')) {
|
||||
element.style.display = 'none';
|
||||
}
|
||||
break
|
||||
default:
|
||||
if (matchMedia("(prefers-color-scheme: light)").matches) {
|
||||
document.body.classList.add("light-theme")
|
||||
document.body.classList.remove("dark-theme")
|
||||
for (const element of document.body.getElementsByClassName('light')) {
|
||||
element.style.display = 'none';
|
||||
}
|
||||
} else {
|
||||
document.body.classList.add("dark-theme")
|
||||
document.body.classList.remove("light-theme")
|
||||
for (const element of document.body.getElementsByClassName('dark')) {
|
||||
element.style.display = 'none';
|
||||
}
|
||||
} else {
|
||||
document.body.classList.add("dark-theme")
|
||||
document.body.classList.remove("light-theme")
|
||||
for (const element of document.body.getElementsByClassName('dark')) {
|
||||
element.style.display = 'none';
|
||||
}
|
||||
}
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
}
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -5,44 +5,29 @@ import utils from "../../../assets/javascripts/utils.js"
|
||||
import generalHelper from "../../../assets/javascripts/general.js"
|
||||
import servicesHelper from "../../../assets/javascripts/services.js"
|
||||
|
||||
let config
|
||||
|
||||
async function getConfig() {
|
||||
return new Promise(resolve => {
|
||||
fetch("/config.json")
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
config = JSON.parse(data)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function setOption(option, type, event) {
|
||||
browser.storage.local.get("options", r => {
|
||||
let options = r.options
|
||||
if (type == "select") {
|
||||
options[option] = event.target.options[event.target.options.selectedIndex].value
|
||||
} else if (type == "checkbox") {
|
||||
options[option] = event.target.checked
|
||||
} else if (type == "range") {
|
||||
options[option] = event.target.value
|
||||
}
|
||||
|
||||
browser.storage.local.set({ options })
|
||||
})
|
||||
async function setOption(option, type, event) {
|
||||
let options = await utils.getOptions()
|
||||
if (type == "select") {
|
||||
options[option] = event.target.options[event.target.options.selectedIndex].value
|
||||
} else if (type == "checkbox") {
|
||||
options[option] = event.target.checked
|
||||
} else if (type == "range") {
|
||||
options[option] = event.target.value
|
||||
}
|
||||
browser.storage.local.set({ options })
|
||||
}
|
||||
|
||||
let exportSettingsElement = document.getElementById("export-settings")
|
||||
|
||||
function exportSettings() {
|
||||
browser.storage.local.get("options", result => {
|
||||
result.options.version = browser.runtime.getManifest().version
|
||||
let resultString = JSON.stringify(result.options, null, " ")
|
||||
exportSettingsElement.href = "data:application/json;base64," + btoa(resultString)
|
||||
exportSettingsElement.download = "libredirect-settings.json"
|
||||
return
|
||||
})
|
||||
async function exportSettings() {
|
||||
const options = await utils.getOptions()
|
||||
options.version = browser.runtime.getManifest().version
|
||||
let resultString = JSON.stringify(options, null, " ")
|
||||
exportSettingsElement.href = "data:application/json;base64," + btoa(resultString)
|
||||
exportSettingsElement.download = "libredirect-settings.json"
|
||||
return
|
||||
}
|
||||
exportSettings()
|
||||
|
||||
@ -62,8 +47,6 @@ importSettingsElement.addEventListener("change", () => {
|
||||
&& data.version == browser.runtime.getManifest().version
|
||||
) {
|
||||
browser.storage.local.clear(async () => {
|
||||
await generalHelper.initDefaults()
|
||||
await servicesHelper.initDefaults()
|
||||
browser.storage.local.set({ options: data }, () => {
|
||||
location.reload()
|
||||
})
|
||||
@ -88,7 +71,6 @@ const resetSettings = document.getElementById("reset-settings")
|
||||
resetSettings.addEventListener("click", async () => {
|
||||
resetSettings.innerHTML = "..."
|
||||
browser.storage.local.clear(async () => {
|
||||
await generalHelper.initDefaults()
|
||||
await servicesHelper.initDefaults()
|
||||
location.reload()
|
||||
})
|
||||
@ -104,25 +86,23 @@ let nameCustomInstanceInput = document.getElementById("exceptions-custom-instanc
|
||||
let instanceTypeElement = document.getElementById("exceptions-custom-instance-type")
|
||||
let instanceType = "url"
|
||||
|
||||
await getConfig()
|
||||
let config = await utils.getConfig()
|
||||
|
||||
for (const service in config.services) {
|
||||
document.getElementById(service).addEventListener("change", event => {
|
||||
browser.storage.local.get("options", r => {
|
||||
let options = r.options
|
||||
if (event.target.checked && !options.popupServices.includes(service)) options.popupServices.push(service)
|
||||
else if (options.popupServices.includes(service)) {
|
||||
var index = options.popupServices.indexOf(service)
|
||||
if (index !== -1) options.popupServices.splice(index, 1)
|
||||
}
|
||||
browser.storage.local.set({ options })
|
||||
})
|
||||
document.getElementById(service).addEventListener("change", async event => {
|
||||
let options = await utils.getOptions()
|
||||
if (event.target.checked && !options.popupServices.includes(service)) options.popupServices.push(service)
|
||||
else if (options.popupServices.includes(service)) {
|
||||
var index = options.popupServices.indexOf(service)
|
||||
if (index !== -1) options.popupServices.splice(index, 1)
|
||||
}
|
||||
browser.storage.local.set({ options })
|
||||
})
|
||||
}
|
||||
|
||||
browser.storage.local.get("options", r => {
|
||||
themeElement.value = r.options.theme
|
||||
let options = r.options
|
||||
!async function () {
|
||||
const options = await utils.getOptions()
|
||||
themeElement.value = options.theme
|
||||
|
||||
instanceTypeElement.addEventListener("change", event => {
|
||||
instanceType = event.target.options[instanceTypeElement.selectedIndex].value
|
||||
@ -134,7 +114,7 @@ browser.storage.local.get("options", r => {
|
||||
nameCustomInstanceInput.setAttribute("placeholder", "https?://(www.|)youtube.com/")
|
||||
}
|
||||
})
|
||||
let exceptionsCustomInstances = r.options.exceptions
|
||||
let exceptionsCustomInstances = options.exceptions
|
||||
function calcExceptionsCustomInstances() {
|
||||
document.getElementById("exceptions-custom-checklist").innerHTML = [...exceptionsCustomInstances.url, ...exceptionsCustomInstances.regex]
|
||||
.map(
|
||||
@ -190,4 +170,4 @@ browser.storage.local.get("options", r => {
|
||||
})
|
||||
|
||||
for (const service in config.services) document.getElementById(service).checked = options.popupServices.includes(service)
|
||||
})
|
||||
}
|
@ -1,35 +1,24 @@
|
||||
"use strict"
|
||||
window.browser = window.browser || window.chrome
|
||||
|
||||
import servicesHelper from "../../assets/javascripts/services.js"
|
||||
import utils from "../../assets/javascripts/utils.js"
|
||||
import serviceHelper from "../../assets/javascripts/services.js"
|
||||
|
||||
let config,
|
||||
divs = {}
|
||||
|
||||
async function getConfig() {
|
||||
return new Promise(resolve => {
|
||||
fetch("/config.json")
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
config = JSON.parse(data)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
config = await utils.getConfig()
|
||||
|
||||
await getConfig()
|
||||
|
||||
utils.switchInstance(true).then(r => {
|
||||
servicesHelper.switchInstance(true).then(r => {
|
||||
if (!r) document.getElementById("change_instance_div").style.display = "none"
|
||||
else document.getElementById("change_instance").addEventListener("click", () => utils.switchInstance(false))
|
||||
else document.getElementById("change_instance").addEventListener("click", () => servicesHelper.switchInstance(false))
|
||||
})
|
||||
|
||||
utils.copyRaw(true).then(r => {
|
||||
servicesHelper.copyRaw(true).then(r => {
|
||||
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))
|
||||
copy_raw.addEventListener("click", () => servicesHelper.copyRaw(false, copy_raw))
|
||||
}
|
||||
})
|
||||
document.getElementById("more-options").addEventListener("click", () => browser.runtime.openOptionsPage())
|
||||
@ -55,7 +44,7 @@ await setDivs()
|
||||
|
||||
const currentSiteIsFrontend = document.getElementById("current_site_divider")
|
||||
|
||||
browser.storage.local.get(["options", "redirects"], r => {
|
||||
browser.storage.local.get(["options"], r => {
|
||||
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
|
||||
for (const service in config.services) {
|
||||
if (!r.options.popupServices.includes(service)) allSites.getElementsByClassName(service)[0].classList.add("hide")
|
||||
@ -76,7 +65,7 @@ browser.storage.local.get(["options", "redirects"], r => {
|
||||
return
|
||||
}
|
||||
|
||||
let service = await serviceHelper.computeService(url, true)
|
||||
let service = await servicesHelper.computeService(url, true)
|
||||
let frontend
|
||||
let instance
|
||||
if (service) {
|
||||
|
@ -411,6 +411,10 @@ body.light-theme a {
|
||||
color: black;
|
||||
}
|
||||
|
||||
body.light-theme a:hover {
|
||||
color: var(--active)
|
||||
}
|
||||
|
||||
section.general {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
|
Loading…
x
Reference in New Issue
Block a user