-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
446 lines (407 loc) · 14.2 KB
/
popup.js
File metadata and controls
446 lines (407 loc) · 14.2 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Small helpers to make storage calls reliably promise-based across Chrome versions
function storageGet(keys) {
return new Promise((resolve) => {
chrome.storage.local.get(keys, (res) => resolve(res || {}));
});
}
function storageSet(items) {
return new Promise((resolve) => {
chrome.storage.local.set(items, () => resolve());
});
}
// Timer logic
const timerEl = document.getElementById('timer');
const startPauseBtn = document.getElementById('start-pause');
let running = false;
let startTime = 0;
let total = 0;
let interval;
function updateTimer() {
const now = Date.now();
const diff = total + (running ? now - startTime : 0);
const s = Math.floor(diff / 1000);
const h = String(Math.floor(s / 3600)).padStart(2,'0');
const m = String(Math.floor((s % 3600) / 60)).padStart(2,'0');
const sec = String(s % 60).padStart(2,'0');
timerEl.textContent = `${h}:${m}:${sec}`;
}
function updateStartPauseUI() {
if (!startPauseBtn) return;
startPauseBtn.textContent = running ? 'Pause' : 'Start';
}
function start() {
if (!running) {
running = true;
startTime = Date.now();
interval = setInterval(updateTimer, 500);
if (timerEl) {
timerEl.classList.add('running');
timerEl.classList.remove('paused');
}
updateStartPauseUI();
}
}
function pause() {
if (running) {
running = false;
total += Date.now() - startTime;
clearInterval(interval);
if (timerEl) {
timerEl.classList.add('paused');
timerEl.classList.remove('running');
}
updateStartPauseUI();
}
}
function reset() {
running = false;
total = 0;
clearInterval(interval);
updateTimer();
if (timerEl) {
timerEl.classList.remove('running');
timerEl.classList.remove('paused');
}
updateStartPauseUI();
}
if (startPauseBtn) {
startPauseBtn.onclick = () => (running ? pause() : start());
}
document.getElementById('reset').onclick = reset;
// Open options/settings from popup
function openOptions() {
if (chrome.runtime.openOptionsPage) chrome.runtime.openOptionsPage();
else window.open('options.html');
}
const settingsBtn = document.getElementById('open-settings');
if (settingsBtn) settingsBtn.addEventListener('click', openOptions);
const settingsFooterBtn = document.getElementById('open-settings-footer');
if (settingsFooterBtn) settingsFooterBtn.addEventListener('click', openOptions);
// Zen Mode toggle (true => blocking ON, break_mode=false)
const zenToggle = document.getElementById('zen-toggle');
const content = document.getElementById('content');
const tabs = document.querySelectorAll('.tab');
const panels = document.querySelectorAll('.panel');
const ACTIVE_PANEL_KEY = 'focusmate_active_panel';
function openCollapsible(el) {
if (!el) return;
el.classList.add('open');
// Set to measured height to animate
const target = el.scrollHeight;
el.style.maxHeight = target + 'px';
// After transition, clear max-height so internal content can grow naturally
const onEnd = (e) => {
if (e.propertyName === 'max-height') {
el.style.maxHeight = 'none';
el.removeEventListener('transitionend', onEnd);
}
};
el.addEventListener('transitionend', onEnd);
}
function closeCollapsible(el) {
if (!el) return;
// Set explicit current height to enable closing transition
el.style.maxHeight = el.scrollHeight + 'px';
// Force reflow so the browser acknowledges the current height before we collapse
void el.offsetHeight;
el.classList.remove('open');
el.style.maxHeight = '0px';
}
function applyContentVisibility(isOn) {
if (!content) return;
if (isOn) openCollapsible(content);
else closeCollapsible(content);
}
function activatePanel(id) {
panels.forEach(p => p.classList.toggle('active', p.id === id));
tabs.forEach(t => {
const active = t.getAttribute('data-target') === id;
t.classList.toggle('active', active);
t.setAttribute('aria-selected', String(active));
});
storageSet({ [ACTIVE_PANEL_KEY]: id });
}
// Topic and blocklist elements
const topicInput = document.getElementById('topic-input');
const addTopicBtn = document.getElementById('add-topic');
const manualBlockInput = document.getElementById('manual-block');
const blockSiteBtn = document.getElementById('block-site');
const blockedListEl = document.getElementById('blocked-list');
const blockCurrentBtn = document.getElementById('block-current');
const topicsListEl = document.getElementById('topics-list');
const TOPICS_KEY = 'focusmate_topics';
// Pomodoro elements
const pomoWorkInput = document.getElementById('pomo-work');
const pomoBreakInput = document.getElementById('pomo-break');
const pomoTimerEl = document.getElementById('pomo-timer');
const pomoStartPauseBtn = document.getElementById('pomo-start-pause');
const pomoResetBtn = document.getElementById('pomo-reset');
const pomoCyclesEl = document.getElementById('pomo-cycles');
const pomoPhaseEl = document.getElementById('pomo-phase');
let pomoRunning = false;
let pomoInterval = null;
let pomoPhase = 'Work'; // 'Work' | 'Break'
let pomoSecondsLeft = 25 * 60;
let pomoCycles = 0;
function intBetween(v, min, max, fallback) {
const n = parseInt(v, 10);
if (Number.isFinite(n)) return Math.min(max, Math.max(min, n));
return fallback;
}
function fmtMMSS(totalSeconds) {
const m = String(Math.floor(totalSeconds / 60)).padStart(2, '0');
const s = String(totalSeconds % 60).padStart(2, '0');
return `${m}:${s}`;
}
function updatePomoUI() {
if (pomoTimerEl) pomoTimerEl.textContent = fmtMMSS(pomoSecondsLeft);
if (pomoCyclesEl) pomoCyclesEl.textContent = String(pomoCycles);
if (pomoPhaseEl) pomoPhaseEl.textContent = pomoPhase;
if (pomoTimerEl) {
pomoTimerEl.classList.toggle('running', pomoRunning);
pomoTimerEl.classList.toggle('paused', !pomoRunning);
}
}
function updatePomoStartPauseUI() {
if (!pomoStartPauseBtn) return;
pomoStartPauseBtn.textContent = pomoRunning ? 'Pause' : 'Start';
}
function setPomoPhase(nextPhase) {
pomoPhase = nextPhase;
const workMin = intBetween(pomoWorkInput?.value || 25, 1, 180, 25);
const breakMin = intBetween(pomoBreakInput?.value || 5, 1, 60, 5);
pomoSecondsLeft = (nextPhase === 'Work' ? workMin : breakMin) * 60;
updatePomoUI();
}
function tickPomodoro() {
if (!pomoRunning) return;
pomoSecondsLeft -= 1;
if (pomoSecondsLeft <= 0) {
if (pomoPhase === 'Work') {
pomoCycles += 1;
setPomoPhase('Break');
} else {
setPomoPhase('Work');
}
}
updatePomoUI();
}
function startPomodoro() {
if (pomoRunning) return;
pomoRunning = true;
if (!pomoInterval) pomoInterval = setInterval(tickPomodoro, 1000);
updatePomoUI();
updatePomoStartPauseUI();
}
function pausePomodoro() {
if (!pomoRunning) return;
pomoRunning = false;
updatePomoUI();
updatePomoStartPauseUI();
}
function resetPomodoro() {
pomoRunning = false;
clearInterval(pomoInterval);
pomoInterval = setInterval(tickPomodoro, 1000); // keep interval for immediate start later
pomoCycles = 0;
setPomoPhase('Work');
updatePomoUI();
updatePomoStartPauseUI();
}
function normalizeDomain(s) {
try {
s = s.trim();
if (!s) return '';
// remove protocol and path
if (!s.startsWith('http')) s = 'https://' + s;
const u = new URL(s);
return u.hostname.replace(/^www\./, '');
} catch {
return s.split('/')[0].replace(/^www\./, '').trim();
}
}
function renderBlocked(list) {
blockedListEl.innerHTML = '';
(Array.isArray(list) ? list : []).forEach(site => {
const li = document.createElement('li');
const span = document.createElement('span');
span.textContent = site;
span.className = 'site';
const removeBtn = document.createElement('button');
removeBtn.textContent = '×';
removeBtn.className = 'remove';
removeBtn.addEventListener('click', async () => {
const { focusmate_blocklist = [] } = await storageGet('focusmate_blocklist');
const next = (focusmate_blocklist || []).filter(s => s !== site);
await storageSet({ focusmate_blocklist: next });
renderBlocked(next);
});
li.appendChild(span);
li.appendChild(removeBtn);
blockedListEl.appendChild(li);
});
}
function renderTopics(list, active) {
if (!topicsListEl) return;
topicsListEl.innerHTML = '';
(Array.isArray(list) ? list : []).forEach(topic => {
const li = document.createElement('li');
const span = document.createElement('span');
span.textContent = topic;
span.className = 'site' + (active && active === topic ? ' active' : '');
span.title = 'Click to set as active topic';
span.addEventListener('click', async () => {
await storageSet({ focusmate_topic: topic });
const data = await storageGet([TOPICS_KEY]);
const topics = Array.isArray(data[TOPICS_KEY]) ? data[TOPICS_KEY] : [];
renderTopics(topics, topic);
topicInput.value = topic;
});
const removeBtn = document.createElement('button');
removeBtn.textContent = '×';
removeBtn.className = 'remove';
removeBtn.addEventListener('click', async (e) => {
e.stopPropagation();
const data = await storageGet([TOPICS_KEY, 'focusmate_topic']);
const topics = Array.isArray(data[TOPICS_KEY]) ? data[TOPICS_KEY] : [];
const next = topics.filter(t => t !== topic);
const updates = { [TOPICS_KEY]: next };
if (data.focusmate_topic === topic) {
updates['focusmate_topic'] = next[0] || '';
topicInput.value = updates['focusmate_topic'] || '';
}
await storageSet(updates);
renderTopics(next, updates['focusmate_topic'] ?? active);
});
li.appendChild(span);
li.appendChild(removeBtn);
topicsListEl.appendChild(li);
});
}
addTopicBtn.addEventListener('click', async () => {
const v = (topicInput.value || '').trim();
if (!v) return;
const data = await storageGet([TOPICS_KEY, 'focusmate_topic']);
const topics = Array.isArray(data[TOPICS_KEY]) ? data[TOPICS_KEY] : [];
const set = new Set(topics);
set.add(v);
const nextTopics = Array.from(set);
await storageSet({ [TOPICS_KEY]: nextTopics, focusmate_topic: v });
renderTopics(nextTopics, v);
});
blockSiteBtn.addEventListener('click', async () => {
const raw = manualBlockInput.value;
if (!raw.trim()) return;
const parts = raw.split(',');
const toAdd = parts.map(normalizeDomain).filter(Boolean);
const { focusmate_blocklist = [] } = await storageGet('focusmate_blocklist');
const set = new Set(focusmate_blocklist);
toAdd.forEach(s => set.add(s));
const next = Array.from(set);
await storageSet({ focusmate_blocklist: next });
manualBlockInput.value = '';
renderBlocked(next);
});
if (blockCurrentBtn) {
blockCurrentBtn.addEventListener('click', async () => {
try {
const tabs = await new Promise(resolve => chrome.tabs.query({ active: true, currentWindow: true }, resolve));
const tab = Array.isArray(tabs) ? tabs[0] : tabs;
const url = tab?.url;
if (!url) return;
let host = '';
try {
const u = new URL(url);
if (u.protocol !== 'http:' && u.protocol !== 'https:') return; // ignore chrome://, file://, etc.
host = u.hostname.replace(/^www\./, '');
} catch {
return;
}
const { focusmate_blocklist = [] } = await storageGet('focusmate_blocklist');
const set = new Set(focusmate_blocklist || []);
set.add(host);
const next = Array.from(set);
await storageSet({ focusmate_blocklist: next });
renderBlocked(next);
} catch (e) {
console.warn('Failed to block current site', e);
}
});
}
async function setZenMode(isOn) {
await storageSet({ focusmate_break_mode: !isOn });
applyContentVisibility(isOn);
if (zenToggle) {
zenToggle.classList.toggle('on', isOn);
}
}
if (zenToggle) {
zenToggle.addEventListener('click', async () => {
const { focusmate_break_mode = false } = await storageGet('focusmate_break_mode');
const isOn = !Boolean(focusmate_break_mode);
setZenMode(!isOn);
});
// Keyboard accessibility
zenToggle.addEventListener('keydown', async (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
const { focusmate_break_mode = false } = await storageGet('focusmate_break_mode');
const isOn = !Boolean(focusmate_break_mode);
setZenMode(!isOn);
}
});
}
// Wire Pomodoro controls
if (pomoStartPauseBtn) pomoStartPauseBtn.addEventListener('click', () => (pomoRunning ? pausePomodoro() : startPomodoro()));
if (pomoResetBtn) pomoResetBtn.addEventListener('click', resetPomodoro);
if (pomoWorkInput) pomoWorkInput.addEventListener('change', () => {
if (!pomoRunning && pomoPhase === 'Work') setPomoPhase('Work');
});
if (pomoBreakInput) pomoBreakInput.addEventListener('change', () => {
if (!pomoRunning && pomoPhase === 'Break') setPomoPhase('Break');
});
// Wire Tabs
tabs.forEach(tab => {
tab.addEventListener('click', () => {
const id = tab.getAttribute('data-target');
if (id) activatePanel(id);
});
});
// Initialize UI with saved values
(async function init() {
try {
const { focusmate_topic = '', focusmate_blocklist = [], focusmate_break_mode = false, [TOPICS_KEY]: topics = [], [ACTIVE_PANEL_KEY]: savedPanel } = await storageGet([
'focusmate_topic',
'focusmate_blocklist',
'focusmate_break_mode',
TOPICS_KEY,
ACTIVE_PANEL_KEY
]);
topicInput.value = focusmate_topic || '';
const isOn = !Boolean(focusmate_break_mode);
// Initialize collapsed/open state without jitter
if (isOn) {
content.classList.add('open');
content.style.maxHeight = 'none';
} else {
content.classList.remove('open');
content.style.maxHeight = '0px';
}
if (zenToggle) zenToggle.classList.toggle('on', isOn);
renderBlocked(Array.isArray(focusmate_blocklist) ? focusmate_blocklist : []);
renderTopics(Array.isArray(topics) ? topics : [], focusmate_topic);
updateTimer();
updateStartPauseUI();
// Initialize Pomodoro defaults
if (pomoWorkInput && !pomoWorkInput.value) pomoWorkInput.value = '25';
if (pomoBreakInput && !pomoBreakInput.value) pomoBreakInput.value = '5';
setPomoPhase('Work');
// prepare ticking; not running until user hits Start
if (!pomoInterval) pomoInterval = setInterval(tickPomodoro, 1000);
updatePomoStartPauseUI();
// Activate saved panel or default to timer
activatePanel(savedPanel || 'panel-timer');
} catch (e) {
console.warn('Failed to init popup state', e);
}
})();