-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
320 lines (282 loc) · 10.6 KB
/
options.js
File metadata and controls
320 lines (282 loc) · 10.6 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const api = globalThis.SessionSentinelApi || {
runtime: chrome.runtime,
storage: chrome.storage
};
const C = globalThis.SESSION_SENTINEL_CONSTANTS || {};
const DEFAULT_SETTINGS = C.DEFAULT_SETTINGS || {
monitoringEnabled: true,
alertSensitivity: "medium",
notificationsEnabled: true,
darkMode: "system",
customPatterns: {}
};
// SVG icon fragments reused in JS-created elements
const ICON_X =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>';
const ICON_CHECK =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>';
const ICON_ALERT =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>';
// ---------------------------------------------------------------------------
// Dark mode — supports "system" | "dark" | "light" (and legacy booleans)
// ---------------------------------------------------------------------------
function resolveDarkMode(setting) {
if (setting === true || setting === "dark") return true;
if (setting === false || setting === "light") return false;
return window.matchMedia("(prefers-color-scheme: dark)").matches;
}
function applyDarkMode(darkModeSetting) {
const isDark = resolveDarkMode(darkModeSetting);
document.documentElement.classList.toggle("dark", isDark);
}
// Re-evaluate when OS theme changes (matters when set to "system")
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", async () => {
const settings = await loadSettings();
const dm = settings.darkMode;
if (dm === "system" || dm === undefined || dm === null) {
applyDarkMode(dm);
}
});
// ---------------------------------------------------------------------------
// Settings I/O
// ---------------------------------------------------------------------------
async function loadSettings() {
try {
const { settings = DEFAULT_SETTINGS } = await api.storage.local.get([
"settings"
]);
return { ...DEFAULT_SETTINGS, ...settings };
} catch (_) {
return { ...DEFAULT_SETTINGS };
}
}
async function saveSettings(settings) {
await api.storage.local.set({ settings });
}
// ---------------------------------------------------------------------------
// Custom pattern editor
// ---------------------------------------------------------------------------
function createPatternRow(domain, cookies, storage) {
domain = domain || "";
cookies = cookies || "";
storage = storage || "";
const row = document.createElement("div");
row.className = "pattern-row";
const domainInput = document.createElement("input");
domainInput.type = "text";
domainInput.placeholder = "domain.com";
domainInput.value = domain;
domainInput.setAttribute("aria-label", "Domain");
const cookieInput = document.createElement("input");
cookieInput.type = "text";
cookieInput.placeholder = "cookie1, cookie2";
cookieInput.value = cookies;
cookieInput.setAttribute("aria-label", "Cookie names");
const storageInput = document.createElement("input");
storageInput.type = "text";
storageInput.placeholder = "key1, key2";
storageInput.value = storage;
storageInput.setAttribute("aria-label", "Storage keys");
const removeBtn = document.createElement("button");
removeBtn.className = "remove-btn";
removeBtn.innerHTML = ICON_X;
removeBtn.title = "Remove pattern";
removeBtn.setAttribute("aria-label", "Remove pattern");
removeBtn.type = "button";
removeBtn.addEventListener("click", () => row.remove());
row.append(domainInput, cookieInput, storageInput, removeBtn);
return row;
}
function loadPatternsIntoUI(customPatterns) {
const container = document.getElementById("customPatterns");
container.replaceChildren();
for (const [domain, pattern] of Object.entries(customPatterns || {})) {
const cookies = (pattern.cookiePatterns || []).join(", ");
const storage = (pattern.storageKeyPatterns || []).join(", ");
container.appendChild(createPatternRow(domain, cookies, storage));
}
}
function collectPatternsFromUI() {
const patterns = {};
const rows = document.querySelectorAll("#customPatterns .pattern-row");
rows.forEach((row) => {
const inputs = row.querySelectorAll("input[type='text']");
const domain = (inputs[0]?.value || "").trim().toLowerCase();
const cookies = (inputs[1]?.value || "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const storage = (inputs[2]?.value || "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
if (domain && (cookies.length || storage.length)) {
patterns[domain] = {
displayName: domain,
cookiePatterns: cookies,
storageKeyPatterns: storage
};
}
});
return patterns;
}
// ---------------------------------------------------------------------------
// Export / Import
// ---------------------------------------------------------------------------
function exportSettingsToFile(settings) {
const blob = new Blob([JSON.stringify(settings, null, 2)], {
type: "application/json"
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download =
"session-sentinel-settings-" +
new Date().toISOString().slice(0, 10) +
".json";
a.click();
URL.revokeObjectURL(url);
}
function importSettingsFromFile() {
return new Promise((resolve, reject) => {
const input = document.getElementById("importFile");
input.value = "";
input.onchange = () => {
const file = input.files?.[0];
if (!file) return reject(new Error("No file selected"));
const reader = new FileReader();
reader.onload = () => {
try {
resolve(JSON.parse(reader.result));
} catch (e) {
reject(new Error("Invalid JSON file: " + e.message));
}
};
reader.readAsText(file);
};
input.click();
});
}
// ---------------------------------------------------------------------------
// Status messages
// ---------------------------------------------------------------------------
function showStatus(msg, isError) {
const el = document.getElementById("status");
el.className = "status-msg visible " + (isError ? "error" : "success");
el.innerHTML =
(isError ? ICON_ALERT : ICON_CHECK) + " <span>" + msg + "</span>";
setTimeout(() => {
el.classList.remove("visible");
}, 3000);
}
// ---------------------------------------------------------------------------
// Dark mode value normaliser (handles legacy booleans)
// ---------------------------------------------------------------------------
function normalizeDarkModeValue(stored) {
if (stored === true) return "dark";
if (stored === false) return "light";
if (stored === "dark" || stored === "light" || stored === "system")
return stored;
return "system";
}
// ---------------------------------------------------------------------------
// Init
// ---------------------------------------------------------------------------
async function initialize() {
const settings = await loadSettings();
// Apply dark mode immediately
applyDarkMode(settings.darkMode);
// Show extension version from manifest
try {
const manifest = api.runtime.getManifest();
document.getElementById("version").textContent = manifest.version;
} catch (_) {
document.getElementById("version").textContent = "\u2013";
}
// Populate form fields
document.getElementById("monitoringEnabled").checked = Boolean(
settings.monitoringEnabled
);
document.getElementById("notificationsEnabled").checked =
settings.notificationsEnabled !== false;
document.getElementById("darkMode").value = normalizeDarkModeValue(
settings.darkMode
);
document.getElementById("alertSensitivity").value =
settings.alertSensitivity || "medium";
loadPatternsIntoUI(settings.customPatterns);
// Live theme preview when changing dropdown
document.getElementById("darkMode").addEventListener("change", (e) => {
applyDarkMode(e.target.value);
});
// Add pattern row
document.getElementById("addPattern").addEventListener("click", () => {
document.getElementById("customPatterns").appendChild(createPatternRow());
});
// Save
document.getElementById("save").addEventListener("click", async () => {
const updated = {
...settings,
monitoringEnabled:
document.getElementById("monitoringEnabled").checked,
notificationsEnabled:
document.getElementById("notificationsEnabled").checked,
darkMode: document.getElementById("darkMode").value,
alertSensitivity: document.getElementById("alertSensitivity").value,
customPatterns: collectPatternsFromUI()
};
await saveSettings(updated);
showStatus("Settings saved successfully.");
});
// Export settings
document
.getElementById("exportSettings")
.addEventListener("click", async () => {
const current = await loadSettings();
exportSettingsToFile(current);
});
// Import settings
document
.getElementById("importSettings")
.addEventListener("click", async () => {
try {
const imported = await importSettingsFromFile();
const merged = { ...DEFAULT_SETTINGS, ...imported };
await saveSettings(merged);
document.getElementById("monitoringEnabled").checked = Boolean(
merged.monitoringEnabled
);
document.getElementById("notificationsEnabled").checked =
merged.notificationsEnabled !== false;
document.getElementById("darkMode").value = normalizeDarkModeValue(
merged.darkMode
);
document.getElementById("alertSensitivity").value =
merged.alertSensitivity || "medium";
loadPatternsIntoUI(merged.customPatterns);
applyDarkMode(merged.darkMode);
showStatus("Settings imported successfully.");
} catch (err) {
showStatus(err.message, true);
}
});
// Reset all data
document.getElementById("resetAll").addEventListener("click", async () => {
if (
!confirm(
"This will clear ALL data including alerts and token history. Continue?"
)
)
return;
try {
await api.storage.local.clear();
await saveSettings({ ...DEFAULT_SETTINGS });
location.reload();
} catch (err) {
showStatus("Reset failed: " + err.message, true);
}
});
}
initialize();