-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.js
115 lines (92 loc) · 3.63 KB
/
menu.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
chrome.storage.local.get({ domains: defaultDomainList, text: defaultWatermarkText }, storage => {
let domains = storage.domains;
(function main() {
populateFormInputWithCurrentDomain();
selectAllTextInDomainInput();
registerFormSubmissionHandler();
renderWatermarkTextField();
renderDomainList();
})();
function populateFormInputWithCurrentDomain() {
chrome.tabs.query({ active: true, currentWindow: true }, ([{ url }]) => {
const anchor = document.createElement("a");
const input = document.getElementById("domain-to-add-field");
anchor.href = url;
input.value = anchor.hostname;
});
}
function selectAllTextInDomainInput() {
setTimeout(() => {
document
.getElementById("domain-to-add-field")
.select();
}, 10); // Adding a small delay avoids a race condition on Chrome: https://stackoverflow.com/a/19498477
}
function registerFormSubmissionHandler() {
document
.getElementById("add-domain-form")
.addEventListener("submit", onFormSubmit);
}
function renderWatermarkTextField() {
const element = document.getElementById("watermark-text-field-input");
element.value = storage.text;
element.addEventListener("input", () => saveWatermarkTextToStorage());
}
function renderDomainList() {
document.getElementById("domain-list").innerHTML = getDomainListHtml();
attachRemoveItemLinkClickListeners();
}
function renderDomainListItem(domain, index) {
return `
<li id="domain-list-item-index-${index}">
<span>${domain}</span>
<a id="domain-list-item-index-${index}-remove-link" href="#" class="remove-link" title="Remove">🗙</a>
</li>
`;
}
function getDomainListHtml() {
return domains.map(renderDomainListItem).join("");
}
function attachRemoveItemLinkClickListeners() {
domains.forEach((_, index) => {
document
.getElementById(`domain-list-item-index-${index}-remove-link`)
.addEventListener("click", () => onRemoveItemLinkClick(index));
});
}
function onFormSubmit(event) {
event.preventDefault();
const inputValue = document.getElementById("domain-to-add-field").value;
const domainToAdd = sanitiseInput(inputValue);
if (!domainToAdd) return;
addDomainToList(domainToAdd);
saveDomainListToStorage();
renderDomainList();
}
function onRemoveItemLinkClick(index) {
removeDomainFromList(index);
saveDomainListToStorage();
renderDomainList();
}
function sanitiseInput(input) {
const container = document.createElement("div");
const textNode = document.createTextNode(input);
container.appendChild(textNode);
return container.innerHTML;
}
function addDomainToList(domain) {
const lowercaseDomain = domain.toLowerCase();
if (domains.includes(lowercaseDomain)) return;
domains.push(lowercaseDomain);
}
function removeDomainFromList(index) {
domains.splice(index, 1);
}
function saveWatermarkTextToStorage() {
const input = document.getElementById("watermark-text-field-input");
chrome.storage.local.set({ text: input.value });
}
function saveDomainListToStorage() {
chrome.storage.local.set({ domains });
}
});