-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
91 lines (83 loc) · 2.42 KB
/
Copy pathoptions.js
File metadata and controls
91 lines (83 loc) · 2.42 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
const FIELDS = [
"apiKey",
"apiBaseUrl",
"apiEndpointPath",
"apiMaxRedirects",
"apiTimeout",
"iconProcessingDuration",
"iconSuccessDuration",
"iconErrorDuration",
"badgeEnabled",
"badgeMode",
"toastOverlayDuration",
"toastOsDuration",
"quotaTotal",
"quotaStartDate",
];
const NUMBER_FIELDS = new Set([
"apiMaxRedirects",
"apiTimeout",
"iconProcessingDuration",
"iconSuccessDuration",
"iconErrorDuration",
"toastOverlayDuration",
"toastOsDuration",
"quotaTotal",
]);
const BOOLEAN_FIELDS = new Set(["badgeEnabled"]);
const BOOLEAN_DEFAULTS = { badgeEnabled: false };
async function loadSettings() {
const data = await chrome.storage.local.get(FIELDS);
for (const key of FIELDS) {
const el = document.getElementById(key);
if (!el) continue;
const val = data[key];
if (BOOLEAN_FIELDS.has(key)) {
el.checked =
val !== undefined ? Boolean(val) : Boolean(BOOLEAN_DEFAULTS[key]);
} else if (el.tagName === "SELECT") {
el.value = val !== undefined && val !== null ? val : el.options[0].value;
} else {
el.value = val !== undefined && val !== null ? val : "";
}
}
}
async function saveSettings(e) {
e.preventDefault();
const values = {};
for (const key of FIELDS) {
const el = document.getElementById(key);
if (!el) continue;
if (BOOLEAN_FIELDS.has(key)) {
values[key] = el.checked;
} else if (NUMBER_FIELDS.has(key)) {
values[key] = el.value === "" ? 0 : parseInt(el.value, 10);
} else {
values[key] = el.value;
}
}
await chrome.storage.local.set(values);
chrome.runtime.sendMessage({ type: "apikey-saved" });
const status = document.getElementById("save-status");
status.textContent = "Saved";
status.classList.add("wgi-visible");
setTimeout(() => status.classList.remove("wgi-visible"), 2000);
}
function setupPasswordToggles() {
for (const btn of document.querySelectorAll(".wgi-toggle-vis")) {
btn.addEventListener("click", () => {
const target = document.getElementById(btn.dataset.target);
if (!target) return;
const isPassword = target.type === "password";
target.type = isPassword ? "text" : "password";
btn.textContent = isPassword ? "Hide" : "Show";
});
}
}
document.addEventListener("DOMContentLoaded", () => {
loadSettings();
setupPasswordToggles();
document
.getElementById("settings-form")
.addEventListener("submit", saveSettings);
});