Browse Source

redo much of the backend javascript

dev
Kyle 10 months ago
parent
commit
308c7ebf35
2 changed files with 77 additions and 94 deletions
  1. +31
    -34
      js/index.js
  2. +46
    -60
      js/options.js

+ 31
- 34
js/index.js View File

@@ -1,46 +1,43 @@
function restore_options() {
browser.storage.sync.get("links", function(result){
if (result.links) {
var quads = document.querySelectorAll('a')
browser.storage.sync.get(["links","colors","mode"], function(result){
console.log("Loading Settings", result)
var quads = document.getElementsByClassName("quadrant")
var style = document.createElement("style")
if (result) {
for (i = 0; i < quads.length; i++) {
if (result.links[i]) {
if (i < result.links.length) {
var link = result.links[i]
if (link.substring(0, 4) === 'http' ? link = 'options.html' : link = 'http://' + link)
quads[i].href = link
if (link.substring(0,4) == "http") {
quads[i].href = link
} else {
quads[i].href = "http://" + link
}
} else {
result.links[i] = ""
quads[i].href = "options.html"
browser.storage.sync.set({links:result.links})
}
}
}
})
browser.storage.sync.get("colors", function(result){
if (result.colors) {
var quads = document.querySelectorAll('a')
var style = document.createElement('style');
for (i = 0; i < quads.length; i++) {
if (result.colors[i]) {
if (i < result.colors.length) {
style.appendChild(
document.createTextNode('.content a:nth-child(' + (i + 1) + '):hover ~ svg polygon { fill: ' + result.colors[i] + '}')
document.createTextNode(".content a:nth-child(" + (i + 1) + "):hover ~ svg polygon { fill: " + result.colors[i] + "}")
)
} else {
result.colors[i] = ""
browser.storage.sync.set({links:result.links})
}
}
document.head.appendChild(style)
}
})
browser.storage.sync.get("mode", function(result){
if (result.mode) {
switch(result.mode) {
case "light":
document.body.classList.remove('dark')
break
case "dark":
document.body.classList.add('dark')
break
default:
break
if (result.mode) {
if (result.mode == "dark") {
document.body.classList.add("dark")
} else {
document.body.classList.remove("dark")
}
} else {
result.mode = "light"
browser.storage.sync.set(mode)
}
} else {
mode = "light"
browser.storage.sync.set({mode:"light"})
document.head.appendChild(style)
}
})
}
document.addEventListener('DOMContentLoaded', restore_options)
document.addEventListener("DOMContentLoaded", restore_options)

+ 46
- 60
js/options.js View File

@@ -14,77 +14,67 @@ function save_options() {
colors.push(input_colors[i].value)
}
browser.storage.sync.set({links:links, colors:colors})
console.log("Settings Saved", links, colors)
console.log("Settings Saved", links, colors, mode)
}
function restore_options() {
var mode
browser.storage.sync.get("mode", function(result){
if (result.mode) {
switch(result.mode) {
case "light":
document.body.classList.remove('dark')
break
case "dark":
document.body.classList.add('dark')
break
default:
break
}
} else {
mode = "light"
browser.storage.sync.set({mode:"light"})
save_options()
}
})
browser.storage.sync.get("links", function(result){
if (result.links) {
var input_links = document.querySelectorAll('input[type="text"]')
browser.storage.sync.get(["links","colors","mode"], function(result){
console.log("Loading Settings", result)
var input_links = document.querySelectorAll('input[type="text"]')
var input_colors = document.querySelectorAll('input[type="color"]')
if (result) {
for (i = 0; i < input_links.length; i++) {
input_links[i].value = result.links[i]
}
}
})
browser.storage.sync.get("colors", function(result){
if (result.colors) {
var input_colors = document.querySelectorAll('input[type="color"]')
for (i = 0; i < input_colors.length; i++) {
if (result.colors[i]) {
if (i < result.links.length) {
input_links[i].value = result.links[i]
} else {
result.links[i] = ""
input_links[i].value = ""
browser.storage.sync.set({links:result.links})
}
if (i < result.colors.length) {
input_colors[i].value = result.colors[i]
} else {
result.colors[i] = ""
browser.storage.sync.set({colors:result.colors})
}
}
if (result.mode) {
if (result.mode == "dark") {
document.body.classList.add("dark")
} else {
document.body.classList.remove("dark")
}
} else {
result.mode = "light"
browser.storage.sync.set(mode)
}
document.head.appendChild(style)
}
})
}
function change_mode() {
browser.storage.sync.get("mode", function(result){
var input_colors = document.querySelectorAll('input[type="color"]')
switch(result.mode) {
case "light":
document.body.classList.add('dark')
mode = "dark"
browser.storage.sync.set({mode:"dark"})
for (i = 0; i < input_colors.length; i++) {
if (input_colors[i].value == "#cfcfcf") {
input_colors[i].value = "#323234"
}
if (result.mode == "dark") {
document.body.classList.remove('dark')
mode = "light"
browser.storage.sync.set({mode:"light"})
for (i = 0; i < input_colors.length; i++) {
if (input_colors[i].value == "#323234") {
input_colors[i].value = "#cfcfcf"
}
save_options()
break
case "dark":
document.body.classList.remove('dark')
mode = "light"
browser.storage.sync.set({mode:"light"})
for (i = 0; i < input_colors.length; i++) {
if (input_colors[i].value == "#323234") {
input_colors[i].value = "#cfcfcf"
}
}
} else {
document.body.classList.add('dark')
mode = "dark"
browser.storage.sync.set({mode:"dark"})
for (i = 0; i < input_colors.length; i++) {
if (input_colors[i].value == "#cfcfcf") {
input_colors[i].value = "#323234"
}
save_options()
break
default:
break
}
}
})
save_options()
})
}
function load() {
var inputs = document.getElementsByTagName('input')
@@ -92,10 +82,6 @@ function load() {
inputs[i].addEventListener('input', debounce(save_options))
}
document.getElementById("mode").addEventListener("click", change_mode)
var icons = document.getElementsByClassName("icon")
for (var i = 0; i < icons.length; i++) {
icons[i].addEventListener("click", reset_default)
}
restore_options()
}
document.addEventListener('DOMContentLoaded', load)

Loading…
Cancel
Save