-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
79 lines (66 loc) · 2.19 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
// background.js
chrome.runtime.onInstalled.addListener(() => {
console.log('DrinkDost installed.');
initializeNotifications();
});
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'local' && changes.userSettings) {
console.log('User settings changed:', changes.userSettings.newValue);
initializeNotifications();
}
});
function initializeNotifications() {
chrome.storage.local.get(['userSettings'], function(result) {
if (!result.userSettings) {
console.log('No user settings found.');
return;
}
const { username, weight, season, hours } = result.userSettings;
console.log('User settings:', username, weight, season, hours);
const waterIntake = calculateWaterIntake(weight, season);
const totalMinutes = hours * 60;
const intervalMinutes = totalMinutes / (waterIntake.adjustedDailyIntake * 1000 / 62);
// const intervalMinutes = 2; // 2-minute interval for notifications
if (intervalMinutes <= 0) {
console.log('Invalid interval minutes:', intervalMinutes);
return;
}
// Clear any existing intervals
if (globalThis.notificationInterval) {
clearInterval(globalThis.notificationInterval);
}
// Schedule notifications
globalThis.notificationInterval = setInterval(() => {
sendNotification(username);
}, intervalMinutes * 60 * 1000); // Convert minutes to milliseconds
});
}
function sendNotification(username) {
console.log(`Sending notification to ${username}`);
chrome.notifications.create({
type: 'basic',
iconUrl: 'icon128.png',
title: 'DrinkDost Reminder',
message: `DrinkDost says to ${username}: Time to drink water!`
});
}
function calculateWaterIntake(weight, season) {
const dailyIntake = weight * 0.035;
let seasonalFactor;
switch (season.toLowerCase()) {
case 'hot':
seasonalFactor = 0.20;
break;
case 'cold':
case 'moderate':
default:
seasonalFactor = 0.00;
break;
}
const adjustedDailyIntake = dailyIntake * (1 + seasonalFactor);
const workdayIntake = (adjustedDailyIntake / 24) * 8;
return {
adjustedDailyIntake: adjustedDailyIntake.toFixed(2),
workdayIntake: workdayIntake.toFixed(2)
};
}