-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
73 lines (64 loc) · 2.22 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
const KEY = "cohostTagGroups";
const extension = (typeof browser === "undefined") ? chrome : browser;
function splitTags(tags) {
return tags.split(/\s*[\n,]\s*/g);
}
function $(selector) {
return document.querySelector(selector);
}
function el(tag, attribs) {
attribs = attribs || {};
const element = document.createElement(tag);
const text = attribs.text;
if (text) {
const textChild = document.createTextNode(text);
// add the text node to the newly created div
element.appendChild(textChild);
}
delete attribs.text;
for (const [key, value] of Object.entries(attribs)) {
element[key] = value;
}
return element;
}
async function loadOptions() {
const res = (await extension.storage.sync.get(KEY) || {})[KEY] || {};
$("#parent").innerHTML = "";
for (const [key, value] of Object.entries(res)) {
const container = el("div", { className: "edit-container" });
const keyEl = el("input", { value: key, type: "text" })
container.appendChild(keyEl);
const valEl = el("textarea", { value: value.join ? value.join("\n") : value });
container.appendChild(valEl);
const deleteButton = el("button", { text: "Delete" });
deleteButton.addEventListener("click", e => {
delete options[key];
saveOptions();
})
container.appendChild(deleteButton);
const editButton = el("button", { text: "Save" });
editButton.addEventListener("click", e => {
options[keyEl.value] = splitTags(valEl.value);
saveOptions();
})
container.appendChild(editButton);
$("#parent").appendChild(container);
}
return res;
}
let options;
async function saveOptions() {
extension.storage.sync.set({
[KEY]: options
});
await loadOptions();
}
(async () => {
options = await loadOptions();
document.querySelector("form").addEventListener("submit", async e => {
e.preventDefault();
options[$("#key").value] = splitTags($("#values").value);
$("#key").value = $("#values").value = "";
await saveOptions();
});
})();