-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
82 lines (76 loc) · 2.63 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var browser = browser || chrome
let switchTab = (newTab, navId) => {
let hide = document.querySelector("#" + navId).parentNode.querySelector("div").children
for (let i = 0; i < hide.length; i++) {
hide[i].style.display = "none"
if (i == hide.length - 1) document.querySelector(newTab).style.display = ""
}
}
document.addEventListener('DOMContentLoaded', () => {
// Resizable inputs
function resizable (el, factor) {
var int = Number(factor) || 7.7;
function resize() {el.style.width = ((el.value.length + 1) * int) + 'px'}
var e = 'keyup,keypress,focus,blur,change'.split(',');
for (var i in e) el.addEventListener(e[i], resize, false);
resize();
}
// Nav
let tabs = document.querySelectorAll("nav span")
for (let i = 0; i < tabs.length; i++) {
tabs[i].addEventListener('click', e => {
document.querySelector("#" + tabs[i].parentElement.id + " .active").className = ""
tabs[i].className = "active"
switchTab(tabs[i].children[0].getAttribute("href"), tabs[i].parentElement.id)
})
}
function setSettingValue(setting, value) {
let elem = document.querySelector("#" + setting)
if (elem.tagName == "A") {
elem.innerHTML = value ? "on" : "off"
}
if (elem.tagName == "INPUT") {
if (elem.getAttribute("type") == "checkbox") {
elem.checked = value
} else {
elem.value = value
resizable(elem, 12)
}
}
}
// Foreach setting
let settingElems = document.querySelectorAll(".setting")
for (let i = 0; i < settingElems.length; i++) {
// Set default+saved setting values
browser.storage.sync.get(settingElems[i].id, settings => {
let isDefined = settings.hasOwnProperty(settingElems[i].id)
if (isDefined) {
setSettingValue(settingElems[i].id, settings[settingElems[i].id])
} else {
setSettingValue(settingElems[i].id, defaultSettings[settingElems[i].id])
}
})
// Listen to settings changes
let eventType
if (settingElems[i].tagName == "A") eventType = "click"
else if (settingElems[i].tagName == "INPUT") eventType = "change"
settingElems[i].addEventListener(eventType, e => {
let elem = document.querySelector("#" + settingElems[i].id)
let newVal
if (elem.tagName == "A") {
newVal = elem.innerHTML == 'on' ? false : true
elem.innerHTML = newVal ? 'on' : 'off'
}
if (elem.tagName == "INPUT") {
if (elem.getAttribute("type") == "checkbox") {
newVal = elem.checked
} else {
newVal = elem.value
}
}
browser.storage.sync.set({
[settingElems[i].id]: newVal
})
})
}
})