-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
115 lines (102 loc) · 3.96 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
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
const NOTIFICATION_MESSAGES = {
blink: "Time to blink your eyes! Look away from the screen for 20 seconds.",
water: "Time to drink some water! Stay hydrated!",
up: "Time to get up and walk around for a few minutes!",
stretch: "Time to do some stretching exercises!",
oneTime: "Your timer is up!"
};
const DEBUG_MODE = false; // Set this to false to disable debug logging
chrome.runtime.onInstalled.addListener(() => {
if (DEBUG_MODE) console.log('Extension installed/updated');
// Initialize default settings
chrome.storage.sync.get([
'blinkEnabled', 'blinkInterval',
'waterEnabled', 'waterInterval',
'upEnabled', 'upInterval',
'stretchEnabled', 'stretchInterval',
'soundEnabled'
], (result) => {
const defaultSettings = {
blinkEnabled: result.blinkEnabled ?? false,
blinkInterval: result.blinkInterval ?? 10,
waterEnabled: result.waterEnabled ?? false,
waterInterval: result.waterInterval ?? 30,
upEnabled: result.upEnabled ?? false,
upInterval: result.upInterval ?? 40,
stretchEnabled: result.stretchEnabled ?? false,
stretchInterval: result.stretchInterval ?? 35,
soundEnabled: result.soundEnabled ?? true
};
if (DEBUG_MODE) console.log('Default settings:', defaultSettings);
chrome.storage.sync.set(defaultSettings);
updateAlarms(defaultSettings);
});
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'updateAlarms') {
updateAlarms(message.settings);
}
if (message.action === 'createOneTimeTimer') {
chrome.alarms.create('oneTime', {
delayInMinutes: message.minutes
});
if (DEBUG_MODE) console.log(`Created one-time timer for ${message.minutes} minutes`);
}
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (DEBUG_MODE) console.log(`Alarm triggered: ${alarm.name}`);
chrome.storage.sync.get(['soundEnabled'], (result) => {
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon128.png',
title: 'Recharge',
message: NOTIFICATION_MESSAGES[alarm.name],
silent: !result.soundEnabled
});
if (DEBUG_MODE) console.log(`Notification created for ${alarm.name}, sound ${result.soundEnabled ? 'enabled' : 'disabled'}`);
if (alarm.name === 'oneTime') {
// Notify popup that timer is complete
chrome.runtime.sendMessage({ action: 'timerComplete' });
} else {
// Restart repeating alarms as before
chrome.storage.sync.get([`${alarm.name}Interval`], (result) => {
if (result[`${alarm.name}Interval`]) {
chrome.alarms.create(alarm.name, {
delayInMinutes: result[`${alarm.name}Interval`]
});
if (DEBUG_MODE) console.log(`Alarm reset: ${alarm.name} for ${result[`${alarm.name}Interval`]} minutes`);
}
});
}
});
});
function updateAlarms(settings) {
// Clear all existing alarms
chrome.alarms.clearAll();
if (DEBUG_MODE) console.log('Cleared all existing alarms');
// Create new alarms based on settings
if (settings.blinkEnabled && settings.blinkInterval > 0) {
chrome.alarms.create('blink', {
delayInMinutes: settings.blinkInterval
});
if (DEBUG_MODE) console.log(`Created blink alarm: ${settings.blinkInterval} minutes`);
}
if (settings.waterEnabled && settings.waterInterval > 0) {
chrome.alarms.create('water', {
delayInMinutes: settings.waterInterval
});
if (DEBUG_MODE) console.log(`Created water alarm: ${settings.waterInterval} minutes`);
}
if (settings.upEnabled && settings.upInterval > 0) {
chrome.alarms.create('up', {
delayInMinutes: settings.upInterval
});
if (DEBUG_MODE) console.log(`Created up alarm: ${settings.upInterval} minutes`);
}
if (settings.stretchEnabled && settings.stretchInterval > 0) {
chrome.alarms.create('stretch', {
delayInMinutes: settings.stretchInterval
});
if (DEBUG_MODE) console.log(`Created stretch alarm: ${settings.stretchInterval} minutes`);
}
}