-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
40 lines (35 loc) · 1.12 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
const regexInput = document.querySelector("#regexes");
/**
* Tests if all the regexes are valid and sets a border if needed.
*/
function inputHandler() {
const lines = regexInput.value.split('\n');
try {
lines.map(string => new RegExp(string));
regexInput.classList.remove("invalid");
} catch (e) {
regexInput.classList.add("invalid")
}
}
/**
* Saves the newline separated list of regex strings to storage and normalizes.
*/
function storeSettings() {
if (regexInput.classList.contains("invalid")) return;
browser.storage.local.set({regexStrings: regexInput.value.split("\n").filter(string => string.length > 0)});
loadSettings();
browser.runtime.sendMessage({
updateSettings: true
})
}
function updateUI(settings) {
regexInput.value = settings.regexStrings.join("\n");
regexInput.rows = settings.regexStrings.length + 1;
}
function loadSettings() {
const settingsPromise = browser.storage.local.get();
settingsPromise.then(updateUI);
}
loadSettings();
regexInput.addEventListener('input', inputHandler);
regexInput.addEventListener('blur', storeSettings);