-
Notifications
You must be signed in to change notification settings - Fork 13
/
options.js
137 lines (112 loc) · 4.73 KB
/
options.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Store the currently selected settings using browser.storage.sync.
*/
function settingsSave(e) {
settingsCurrent = {
saturation: document.querySelector("#saturation").value,
lightness: document.querySelector("#lightness").value,
colors: document.querySelector("#colors").value,
activeSaturate: document.querySelector("#active-saturate").value,
activeBrightness: document.querySelector("#active-brightness").value,
activeBold: document.querySelector("#active-bold").checked,
hoverSaturate: document.querySelector("#hover-saturate").value,
hoverBrightness: document.querySelector("#hover-brightness").value,
};
settingsCurrent.hosts = [];
document.querySelectorAll("table.hosts tbody tr").forEach(function(hostsItem) {
settingsCurrent.hosts.push({
host: hostsItem.querySelector("input[name='host[]']").value,
regexp: hostsItem.querySelector("input[name='regexp[]']").checked ? true : false,
color: hostsItem.querySelector("input[name='color[]']").value,
disabled: hostsItem.querySelector("input[name='disabled[]']").checked ? true : false,
});
});
browser.storage.sync.clear().then(function() {
browser.storage.sync.set(settingsCurrent).then(function() {
ColoredTabs.init();
}, onError);
});
e.preventDefault();
}
function settingsReset(e) {
let settings = DEFAULT_SETTINGS;
settingsFormFill(settings);
e.preventDefault();
}
/*
Update the options UI with the settings values retrieved from storage,
or the default settings if the stored settings are empty.
*/
function settingsFormFill(settings) {
// console.log(settings);
document.querySelector("#saturation").value = settings.saturation || "";
document.querySelector("#lightness").value = settings.lightness || "";
document.querySelector("#colors").value = settings.colors || "";
document.querySelector("#active-saturate").value = settings.activeSaturate || "";
document.querySelector("#active-brightness").value = settings.activeBrightness || "";
document.querySelector("#active-bold").checked = settings.activeBold || "";
document.querySelector("#hover-saturate").value = settings.hoverSaturate || "";
document.querySelector("#hover-brightness").value = settings.hoverBrightness || "";
document.querySelector("table.hosts tbody").innerHTML = '';
if(settings.hosts) {
settings.hosts.forEach(function(hostsItem) {
hostsAdd(null, hostsItem);
});
} else {
hostsAdd(null, {});
}
// const hostsTemplate = document.querySelector("template.host-item table tbody tr");
}
function hostsAdd(hostsItemPrev = null, data = {}) {
const hostsTemplate = Sanitizer.createSafeHTML(document.querySelector("template").innerHTML);
// console.log(hostsTemplate);
// hostsItem.className = "row";
// box.setAttribute("draggable", true);
let hostsItem = document.createElement('tr');
hostsItem.insertAdjacentHTML('beforeend', Sanitizer.unwrapSafeHTML(hostsTemplate));
hostsItem.querySelector("input[name='host[]']").value = data.host || '';
hostsItem.querySelector("input[name='regexp[]']").checked = data.regexp || false;
hostsItem.querySelector("input[name='color[]']").value = data.color || '#00ff00';
hostsItem.querySelector("input[name='disabled[]']").checked = data.disabled ? true : false;
// hostsItem = hostsTemplate.cloneNode(true);
// console.log(hostsItem)
// // hostsItem = document.createElement('tr');
// // hostsItem.innerHTML = '<td>xxxx2 sda</td>';
// document.querySelector("table.hosts tbody").appendChild(hostsTemplate);
document.querySelector("table.hosts tbody").appendChild(hostsItem);
if (hostsItemPrev) {
hostsItemPrev.parentNode.insertBefore(hostsItem, hostsItemPrev.nextSibling);
}
else {
document.querySelector("table.hosts tbody").appendChild(hostsItem);
}
}
function onError(e) {
console.error(e);
}
/*
On opening the options page, fetch stored settings and update the UI with them.
*/
// settingsReset();
// let settingsStored = ColoredTabs.settingsLoad();
// settingsFormFill(settingsStored);
browser.storage.sync.get().then(function(settingsSaved) {
settings = {}
Object.assign(settings, DEFAULT_SETTINGS, settingsSaved);
settingsFormFill(settings);
});
/*
On submit, save the currently selected settings.
*/
document.querySelector("#save-button").addEventListener("click", settingsSave);
document.querySelector("#reset-button").addEventListener("click", settingsReset);
document.querySelector("table.hosts").addEventListener("click", (e) => {
if (e.target.className == "browser-style add-button") {
hostsAdd(e.target.parentNode.parentNode);
} else if (e.target.className == "browser-style del-button") {
e.target.parentNode.parentNode.remove();
if (document.querySelectorAll("table.hosts tbody tr").length == 0) {
hostsAdd();
}
}
});