-
Notifications
You must be signed in to change notification settings - Fork 17
/
options.js
66 lines (60 loc) · 2.18 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
let examples = {
'name': 'Prod',
'email': 'Prod@example.com',
'number': '123456',
'role': 'InfraEng',
'subdomain': 'CompuGlobalHyperMegaNet'
};
function saveOptions(e) {
e.preventDefault();
browser.storage.sync.set({
template: document.querySelector("#template").value,
length: document.querySelector("#length").value || 0,
slug: document.querySelector("#slug").value
});
}
function populatePreview(text, length, slug) {
for (const [key, value] of Object.entries(examples)) {
text = text.replace(key, value);
}
if (slug) {
text = text.replaceAll(slug, "");
}
if (length && text.length > length && length > 0) {
text = text.substring(0, length - 2) + "...";
}
document.querySelector("#preview").value = text;
}
function restoreOptions() {
function setCurrentChoice(result) {
let text = result.template || "name role";
let length = parseInt(result.length, 10);
let slug = result.slug || "";
document.querySelector("#template").value = text;
document.querySelector("#length").value = length || 0;
document.querySelector("#slug").value = slug;
populatePreview(text, length, slug);
}
function onError(error) {
console.log(`Error: ${error}`);
}
let getting = browser.storage.sync.get(["template", "length", "slug"]);
getting.then(setCurrentChoice, onError);
}
document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);
document.querySelector("#template").addEventListener("input", function(evt) {
let length = document.querySelector("#length").value;
let slug = document.querySelector("#slug").value || "";
populatePreview(this.value, length, slug);
});
document.querySelector("#length").addEventListener("input", function(evt) {
let template = document.querySelector("#template").value;
let slug = document.querySelector("#slug").value || "";
populatePreview(template, this.value, slug);
});
document.querySelector("#slug").addEventListener("input", function(evt) {
let template = document.querySelector("#template").value;
let length = document.querySelector("#length").value;
populatePreview(template, length, this.value || "");
});