-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
99 lines (85 loc) · 3.27 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
// config in `about:addons`
function isValidTime(timeStr) {
// todo
//return /\d\d/.test();
return true;
}
function saveOptions() {
var rules = [];
var sites = document.querySelectorAll("input[name='domain[]']");
var denyFrom = document.querySelectorAll("input[name='deny_start[]']");
var denyTo = document.querySelectorAll("input[name='deny_end[]']");
sites.forEach(function(v, i) {
var site = v.value.trim();
var timeFrom = denyFrom[i].value.trim();
var timeTo = denyTo[i].value.trim();
if (site && isValidTime(timeFrom) && isValidTime(timeTo)) {
rules.push({
site: site,
timeFrom: timeFrom,
timeTo: timeTo
});
}
});
if (rules.length) {
browser.storage.local.set({acdn_rules: rules});
}
}
function restoreOptions() {
function onError(error) {
alert("Error happened, open console to see the text");
console.log("Error restoring options: ", error);
}
function createRulesItems(result) {
var rules = result.acdn_rules || null;
var rulesBlock = document.querySelector("#rules_block");
if (rules) {
var node = document.querySelector(".rule_item");
for (var k in rules) {
if (k == 0) {
node.querySelector("input[name='domain[]']").value = rules[k].site;
node.querySelector("input[name='deny_start[]']").value = rules[k].timeFrom;
node.querySelector("input[name='deny_end[]']").value = rules[k].timeTo;
} else {
var nodeClone = node.cloneNode(true);
nodeClone.querySelector("input[name='domain[]']").value = rules[k].site;
nodeClone.querySelector("input[name='deny_start[]']").value = rules[k].timeFrom;
nodeClone.querySelector("input[name='deny_end[]']").value = rules[k].timeTo;
rulesBlock.appendChild(nodeClone);
}
}
}
}
var rules = browser.storage.local.get("acdn_rules");
rules.then(createRulesItems, onError);
}
function addRule() {
var node = document.querySelector(".rule_item");
var nodeClone = node.cloneNode(true);
var inputs = nodeClone.querySelectorAll("input");
for (var k in inputs) {
inputs[k].value = "";
}
document.querySelector("#rules_block").appendChild(nodeClone);
}
function removeRule(e) {
if (e.target && e.target.className == "remove_rule") {
var ruleParent = e.target.closest(".rule_item");
if (ruleParent) {
var rulesLen = document.querySelectorAll(".rule_item").length;
if (1 < rulesLen) {
ruleParent.parentNode.removeChild(ruleParent);
} else {
var inputs = ruleParent.querySelectorAll("input");
for (var k in inputs) {
inputs[k].value = "";
}
}
}
e.preventDefault();
}
}
document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("#add_rule").addEventListener("click", addRule)
document.addEventListener("click", removeRule)
document.querySelector("form").addEventListener("submit", saveOptions);