-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
236 lines (210 loc) · 8.33 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Default options
let options = {
groupThreshold: 4,
altDomains: [
["^www.google.com/search.*$", "search.google.com"],
["^www.google.com/maps.*$", "maps.google.com"]
],
groupColors: [
["google", "blue"],
["stackoverflow", "orange"],
["duckduckgo", "red"],
],
};
// Used for string comparisons.
const collator = new Intl.Collator();
const controller = {
decreaseGroupThreshold: async function() {
if (options.groupThreshold > 2) {
options.groupThreshold--;
this.storeOptions();
}
},
increaseGroupThreshold: async function() {
if (options.groupThreshold < 99) {
options.groupThreshold++;
this.storeOptions();
}
},
addAltDomain: async function(pattern, domain) {
if (pattern == '' && domain == '') {
// Do nothing.
return;
}
// Validate the pattern.
let compiledPattern = null;
try {
compiledPattern = new RegExp(pattern);
} catch(err) {
ui.showAltDomainErrorMessage(true, ui.INVALID_ALT_DOMAIN_PATTERN);
return;
}
// Validate the replacement domain.
const validDomain = new RegExp("^[a-zA-Z0-9_]+(.[a-zA-Z0-9_]+)*$");
if (domain.match(validDomain) == null) {
ui.showAltDomainErrorMessage(true, ui.INVALID_ALT_DOMAIN_DOMAIN);
return;
}
// Save the alt domain in the options. Replace existing rule if any.
let foundEntry = false;
for (const entry of options.altDomains) {
if (collator.compare(pattern, entry[0]) == 0) {
foundEntry = true;
entry[1] = domain;
}
}
if (!foundEntry) {
options.altDomains.push([pattern.trim(), domain.trim()]);
}
this.storeOptions();
// Hide the error message.
ui.showAltDomainErrorMessage(false);
},
removeAltDomain: async function(pattern) {
for (let i = 0; i < options.altDomains.length; i++) {
if (collator.compare(pattern, options.altDomains[i][0]) == 0) {
options.altDomains.splice(i, 1);
}
}
this.storeOptions();
},
addGroupColor: async function(groupName, color) {
if (groupName) {
let foundEntry = false;
for (const entry of options.groupColors) {
if (collator.compare(groupName, entry[0]) == 0) {
foundEntry = true;
entry[1] = color;
}
}
if (!foundEntry) {
options.groupColors.push([groupName, color]);
}
this.storeOptions();
}
},
removeGroupColor: async function(groupName) {
if (groupName) {
for (let i = 0; i < options.groupColors.length; i++) {
if (collator.compare(groupName, options.groupColors[i][0]) == 0) {
options.groupColors.splice(i, 1);
}
}
this.storeOptions();
}
},
loadOptions: async function() {
//await chrome.storage.sync.clear();
const data = await chrome.storage.sync.get("options");
Object.assign(options, data.options);
console.log("LOAD OPTIONS:", options);
},
storeOptions: async function() {
console.log("STORE OPTIONS:", options);
chrome.storage.sync.set({ options });
},
};
const ui = {
groupThreshold: document.getElementById("groupThreshold"),
decreaseGroupThreshold: document.getElementById("decreaseGroupThreshold"),
increaseGroupThreshold: document.getElementById("increaseGroupThreshold"),
altDomainAdd: document.getElementById("altDomainAdd"),
altDomainPattern: document.getElementById("altDomainPattern"),
altDomainDomain: document.getElementById("altDomainDomain"),
altDomainErrorMesage: document.getElementById("altDomainErrorMessage"),
altDomainList: document.getElementById("altDomainList"),
altDomainTemplate: document.getElementById("altDomainTemplate"),
newGroupColorAdd: document.getElementById("newGroupColorAdd"),
newGroupColorColor: document.getElementById("newGroupColorColor"),
newGroupColorName: document.getElementById("newGroupColorName"),
groupColorList: document.getElementById("groupColorList"),
groupColorTemplate: document.getElementById("groupColorTemplate"),
INVALID_ALT_DOMAIN_PATTERN: "Alternate domain pattern is not a valid regular expression.",
INVALID_ALT_DOMAIN_DOMAIN: "Alternate domain replacement is not a valid domain name.",
updateAll: function() {
this.groupThreshold.textContent = String(options.groupThreshold);
this.updateAltDomains();
this.updateGroupColorSelect();
this.updateGroupColors();
},
updateAltDomains: function() {
this.altDomainList.textContent = '';
const entries = Array.from(options.altDomains);
entries.sort((e1, e2) => collator.compare(e1[0], e2[0]));
for (const altDomain of entries) {
const pattern = altDomain[0];
const domain = altDomain[1];
this.addAltDomain(pattern, domain);
}
},
addAltDomain: function(pattern, domain) {
const listItem = this.altDomainTemplate.content.firstElementChild.cloneNode(true);
const button = listItem.querySelector("input[type='button']");
const label = listItem.querySelector("label");
label.textContent = pattern + " → " + domain;
this.altDomainList.append(listItem);
button.addEventListener("click", () => {
controller.removeAltDomain(pattern);
this.altDomainList.removeChild(listItem);
});
},
showAltDomainErrorMessage(visible, message = "") {
this.altDomainErrorMesage.textContent = message;
this.altDomainErrorMesage.style.display = (visible ? 'block' : 'none');
},
updateGroupColorSelect: function() {
const value = this.newGroupColorColor.value;
const color = "var(--color-tab-" + value + ")";
this.newGroupColorColor.style.backgroundColor = color;
},
updateGroupColors: function() {
this.groupColorList.textContent = '';
const entries = Array.from(options.groupColors);
entries.sort((e1, e2) => collator.compare(e1[0], e2[0]));
for (const groupColor of entries) {
const groupName = groupColor[0];
const color = groupColor[1];
this.addGroupColor(groupName, color);
}
},
addGroupColor: function(groupName, color) {
const listItem = this.groupColorTemplate.content.firstElementChild.cloneNode(true);
const button = listItem.querySelector("input[type='button']");
const label = listItem.querySelector("label");
label.textContent = groupName;
label.classList.add(color);
this.groupColorList.append(listItem);
button.addEventListener("click", () => {
controller.removeGroupColor(groupName);
this.groupColorList.removeChild(listItem);
});
},
addEventListeners: function() {
this.decreaseGroupThreshold.addEventListener("click", async () => {
await controller.decreaseGroupThreshold();
this.groupThreshold.textContent = String(options.groupThreshold);
});
this.increaseGroupThreshold.addEventListener("click", async () => {
await controller.increaseGroupThreshold();
this.groupThreshold.textContent = String(options.groupThreshold);
});
this.altDomainAdd.addEventListener("click", async () => {
const pattern = this.altDomainPattern.value;
const domain = this.altDomainDomain.value;
await controller.addAltDomain(pattern, domain);
this.updateAltDomains();
});
this.newGroupColorColor.addEventListener("change", (event) => {
this.updateGroupColorSelect();
});
this.newGroupColorAdd.addEventListener("click", async () => {
const groupName = this.newGroupColorName.value;
const color = this.newGroupColorColor.value;
await controller.addGroupColor(groupName, color);
this.updateGroupColors();
});
}
};
await controller.loadOptions();
ui.updateAll();
ui.addEventListeners();