Repository navigation
Expand file tree
/
Copy pathoptions.js
More file actions
97 lines (83 loc) · 3.27 KB
/
Copy pathoptions.js
File metadata and controls
97 lines (83 loc) · 3.27 KB
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
const visibilityClass = 'is-visible';
const OPTIONS_LOG_PREFIX = '[NCC options]';
async function saveOptions(e) {
e.preventDefault();
hideMessages();
document.querySelectorAll(".invalid").forEach(el => el.classList.remove("invalid"));
let onColor = document.querySelector(".cookie-ncc-on-color:checked");
let offColor = document.querySelector(".cookie-ncc-off-color:checked");
let nameInput = document.querySelector(".cookie_ncc");
// Storage keys are the same as in version 0.1
let options = {
cookie_ncc: nameInput.value.trim(),
cookie_ncc_on_color: onColor ? onColor.value : DEFAULT_ON_COLOR,
cookie_ncc_off_color: offColor ? offColor.value : DEFAULT_OFF_COLOR
};
let errors = [];
if (options.cookie_ncc && !isValidCookieName(options.cookie_ncc)) {
errors.push(t('optInvalidName', options.cookie_ncc));
nameInput.classList.add("invalid");
}
if (errors.length) {
console.debug(OPTIONS_LOG_PREFIX, 'not saved, invalid values:', errors);
showError(errors.join(' '));
return;
}
console.debug(OPTIONS_LOG_PREFIX, 'saving', options);
try {
await requireApi('storage.' + SETTINGS_AREA + '.set', [options]);
showSuccess();
} catch (err) {
showError(err);
}
}
async function restoreOptions() {
let trigger = (await getTriggerSettings())[0];
let colors = await getColors();
console.debug(OPTIONS_LOG_PREFIX, 'restored', trigger, colors);
// The placeholder "?????_NCC" saved by version 0.1 is shown as the default name
document.querySelector(".cookie_ncc").value = trigger.globalName;
document.querySelector(".cookie-ncc-on-color[value=" + colors.on + "]").checked = true;
document.querySelector(".cookie-ncc-off-color[value=" + colors.off + "]").checked = true;
await renderPermission();
}
async function renderPermission() {
let permitted = await hasHostPermission();
console.debug(OPTIONS_LOG_PREFIX, 'host permission:', permitted);
document.querySelector(".permission-row").hidden = permitted;
}
async function onGrantClick() {
try {
await requestHostPermission();
} catch (err) {
console.error(OPTIONS_LOG_PREFIX, 'permission request failed', err);
}
await renderPermission();
}
function showSuccess() {
let messageClass = document.querySelector(".saved-successfully").classList;
messageClass.add(visibilityClass);
setTimeout(function() {
hideMessages();
}, 2000);
}
// message - validation text; without it (storage error) the generic text is shown
function showError(message) {
let element = document.querySelector(".saved-error");
if (typeof message === 'string') {
element.textContent = message;
} else {
console.error(OPTIONS_LOG_PREFIX, 'save failed', message);
element.textContent = t('optSaveError');
}
element.classList.add(visibilityClass);
}
function hideMessages() {
document.querySelectorAll(".message").forEach(function(message) {
message.classList.remove(visibilityClass);
});
}
localizePage();
document.addEventListener('DOMContentLoaded', restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);
document.querySelector(".grant").addEventListener("click", onGrantClick);