-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
55 lines (48 loc) · 1.37 KB
/
background.js
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
// Background service worker for TaskTimer Pro
const activeTimers = new Map();
chrome.runtime.onInstalled.addListener(() => {
console.log('TaskTimer Pro installed');
});
// Handle messages from the popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'START_TIMER') {
const { task } = message;
activeTimers.set(task.id, task);
updateBadge('⏱️');
return true;
}
if (message.type === 'PAUSE_TIMER') {
const { task } = message;
activeTimers.set(task.id, task);
updateBadge('⏸️');
return true;
}
if (message.type === 'RESUME_TIMER') {
const { task } = message;
activeTimers.set(task.id, task);
updateBadge('⏱️');
return true;
}
if (message.type === 'STOP_TIMER') {
const { taskId } = message;
activeTimers.delete(taskId);
updateBadge('');
return true;
}
});
// Keep service worker alive
chrome.alarms.create('keepAlive', { periodInMinutes: 1 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'keepAlive') {
if (activeTimers.size > 0) {
const timer = Array.from(activeTimers.values())[0];
updateBadge(timer.status === 'paused' ? '⏸️' : '⏱️');
}
}
});
function updateBadge(text) {
chrome.action.setBadgeText({ text });
if (text) {
chrome.action.setBadgeBackgroundColor({ color: '#4F46E5' });
}
}