-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.js
182 lines (115 loc) · 4.63 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
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
const inputDefaultSearchEngine = document.getElementById('input-default-search-engine');
const sectionVariablesContainer = document.getElementById('section-variables');
const textVariableStatus = document.getElementById('p-variables-status');
function onDeleteAction(ruleRow, ruleId) {
browser.storage.sync.get([STORAGE_RULES])
.then(result => {
// Remove entry
// We need to 'find' it because the indices will change after removing elements.
const rmIndex = result[STORAGE_RULES].findIndex(rule => rule.id === ruleId);
result[STORAGE_RULES].splice(rmIndex, 1);
browser.storage.sync.set({ [STORAGE_RULES]: result[STORAGE_RULES] })
.then(() => {
onStorageSet();
// On success, remove the entry from the DOM
ruleRow.remove();
// Provide some feedback just in case
if(result[STORAGE_RULES].length === 0) {
textVariableStatus.innerText = browser.i18n.getMessage("noVariables");
textVariableStatus.style.display = "block";
}
});
});
}
function createRuleRow(ruleId, searchValue, replaceValue) {
// Add row for this entry
const template = `<div class="three columns">
<input class="u-full-width" type="text" id="input-variable-${ruleId}" placeholder="${browser.i18n.getMessage("variableName")}" value="${searchValue}">
</div>
<div class="six columns">
<input class="u-full-width" type="text" id="input-replace-${ruleId}" placeholder="${browser.i18n.getMessage("replaceValue")}" value="${replaceValue}">
</div>
<div class="three columns">
<button id="button-delete-${ruleId}" class="u-full-width">${browser.i18n.getMessage("delete")}</button>
</div>`;
const htmlEntry = document.createElement('div');
htmlEntry.classList.add('row');
htmlEntry.id = `row-variables-${ruleId}`;
htmlEntry.innerHTML = template; // Careful with injection? Do we care here?
sectionVariablesContainer.appendChild(htmlEntry);
return {
ruleRow: htmlEntry,
inputVariable: document.getElementById(`input-variable-${ruleId}`),
inputReplace: document.getElementById(`input-replace-${ruleId}`),
buttonDelete: document.getElementById(`button-delete-${ruleId}`),
};
}
function addInputListeners(inputVariable, inputReplace, ruleId) {
function createOrUpdateEntry(result) {
// 1. create or update searchValue
const ruleIndex = result[STORAGE_RULES].findIndex(rule => rule.id === ruleId);
// -1 means findIndex couldnt find the index
if(ruleIndex === -1) {
result[STORAGE_RULES].push({
id: ruleId,
searchValue: inputVariable.value,
replaceValue: inputReplace.value,
});
} else {
result[STORAGE_RULES][ruleIndex].searchValue = inputVariable.value;
result[STORAGE_RULES][ruleIndex].replaceValue = inputReplace.value;
}
// 2. Sync
browser.storage.sync.set({ [STORAGE_RULES]: result[STORAGE_RULES] })
.then(onStorageSet);
}
function handleChange(event) {
browser.storage.sync.get([STORAGE_RULES])
.then(createOrUpdateEntry);
}
inputVariable.addEventListener("change", handleChange);
inputReplace.addEventListener("change", handleChange);
}
function initSettings() {
// Retrieve everything
browser.storage.sync.get()
.then(result => {
// Search engine query URL
inputDefaultSearchEngine.value = result[STORAGE_DEFAULT_SEARCH_URL];
inputDefaultSearchEngine.addEventListener("change", event => {
browser.storage.sync.set({ [STORAGE_DEFAULT_SEARCH_URL]: inputDefaultSearchEngine.value })
.then(onStorageSet);
});
if(result[STORAGE_RULES].length === 0) {
textVariableStatus.innerText = browser.i18n.getMessage("noVariables");
} else {
textVariableStatus.style.display = "none";
// Variables
for(let i = 0; i < result[STORAGE_RULES].length; i++) {
const ruleId = result[STORAGE_RULES][i].id;
// Add rule row
const {ruleRow, inputVariable, inputReplace, buttonDelete} = createRuleRow(ruleId, result[STORAGE_RULES][i].searchValue, result[STORAGE_RULES][i].replaceValue);
// Add listeners
addInputListeners(inputVariable, inputReplace, ruleId)
// Add listener for delete button
buttonDelete.addEventListener('click', (e) => {
onDeleteAction(ruleRow, ruleId);
});
}
}
});
}
initSettings();
// Init the Add-Variable button
document.getElementById('button-add-variable').addEventListener('click', e => {
// Just in case
textVariableStatus.style.display = "none";
const ruleId = Date.now();
const {ruleRow, inputVariable, inputReplace, buttonDelete} = createRuleRow(ruleId, "", "");
// first make function for adding listeners
// call it here
addInputListeners(inputVariable, inputReplace, ruleId);
buttonDelete.addEventListener('click', (e) => {
onDeleteAction(ruleRow, ruleId);
});
});