-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
63 lines (55 loc) · 1.69 KB
/
options.js
File metadata and controls
63 lines (55 loc) · 1.69 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
const input = document.getElementById('api-key');
const save = document.getElementById('save');
const clear = document.getElementById('clear');
const toggle = document.getElementById('toggle-visibility');
const toggleLabel = document.getElementById('toggle-label');
const status = document.getElementById('status');
function setStatus(txt, ms = 2000) {
status.textContent = txt || '';
if (ms && txt) setTimeout(() => (status.textContent = ''), ms);
}
chrome.storage.local.get('focusmate_api_key', data => {
input.value = data.focusmate_api_key || '';
});
function applyToggle(on) {
input.type = on ? 'text' : 'password';
if (toggle) {
toggle.classList.toggle('on', on);
toggle.setAttribute('aria-checked', String(on));
}
}
if (toggle) {
toggle.addEventListener('click', () => {
const isOn = toggle.classList.contains('on');
applyToggle(!isOn);
});
toggle.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
const isOn = toggle.classList.contains('on');
applyToggle(!isOn);
}
});
}
if (toggleLabel) {
toggleLabel.addEventListener('click', () => {
const isOn = toggle.classList.contains('on');
applyToggle(!isOn);
});
}
// initialize toggle state based on current input type
applyToggle(input?.type === 'text');
save.onclick = () => {
const v = input.value.trim();
if (!v) return setStatus('Enter a non-empty key');
chrome.storage.local.set({ focusmate_api_key: v }, () => {
setStatus('Saved');
});
};
clear.onclick = () => {
if (!confirm('Clear saved API key?')) return;
chrome.storage.local.remove('focusmate_api_key', () => {
input.value = '';
setStatus('Cleared');
});
};