-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground-core.js
More file actions
177 lines (152 loc) · 5.71 KB
/
background-core.js
File metadata and controls
177 lines (152 loc) · 5.71 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
let currentTimer;
let paused = false;
let remainingTime = 0;
let timerName = ""; // To keep track of which timer is currently running
let startTime; // Variable to track when the timer started
let pausedTime; // Variable to track when the timer was paused
let FOCUS_DURATION = 25 * 60 * 1000; // Default 25 minutes in milliseconds
let BREAK_DURATION = 5 * 60 * 1000; // Default 5 minutes in milliseconds
function updateDurations() {
chrome.storage.sync.get(['focusDuration', 'breakDuration'], (data) => {
FOCUS_DURATION = (data.focusDuration || 25 * 60) * 1000; // Convert to milliseconds
BREAK_DURATION = (data.breakDuration || 5 * 60) * 1000; // Convert to milliseconds
});
}
// Call this function when the background script starts
updateDurations();
function showNotification(title, message) {
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon128.png', // Ensure you have an icon.png in your extension directory
title: title,
message: message,
priority: 2,
silent: false // Play default notification sound
});
}
function startFocusSession(duration) {
clearTimeout(currentTimer);
paused = false;
remainingTime = duration * 1000; // Convert to milliseconds
timerName = "focusTimer";
startTime = Date.now();
let endTime = startTime + remainingTime;
function updateTimer() {
if (paused) {
return;
}
let timeLeft = Math.round((endTime - Date.now()) / 1000);
if (timeLeft <= 0) {
chrome.runtime.sendMessage({
action: "timerComplete",
message: "Focus Session Complete! Time for a break!"
});
remainingTime = BREAK_DURATION / 1000; // Prepare for the break timer
showNotification("Focus Session Complete!", "Time for a break!");
startBreak(BREAK_DURATION / 1000); // Start break timer
} else {
chrome.runtime.sendMessage({
action: "updateTimer",
time: formatTime(timeLeft)
});
currentTimer = setTimeout(updateTimer, 1000);
}
}
updateTimer();
}
function startBreak(duration) {
clearTimeout(currentTimer);
paused = false;
remainingTime = duration * 1000; // Convert to milliseconds
timerName = "breakTimer";
startTime = Date.now();
let endTime = startTime + remainingTime;
function updateTimer() {
if (paused) {
return;
}
let timeLeft = Math.round((endTime - Date.now()) / 1000);
if (timeLeft <= 0) {
chrome.runtime.sendMessage({
action: "timerComplete",
message: "Break Time Over! Back to work!"
});
remainingTime = FOCUS_DURATION / 1000; // Prepare for the focus timer
showNotification("Break Time Over!", "Back to work!");
startFocusSession(FOCUS_DURATION / 1000); // Start focus timer
} else {
chrome.runtime.sendMessage({
action: "updateTimer",
time: formatTime(timeLeft)
});
currentTimer = setTimeout(updateTimer, 1000);
}
}
updateTimer();
}
function pauseTimer() {
paused = true;
clearTimeout(currentTimer);
pausedTime = Date.now();
remainingTime = Math.max(0, remainingTime - (pausedTime - startTime));
}
function resumeTimer() {
if (!paused) return; // Don't resume if not paused
paused = false;
let pauseDuration = Date.now() - pausedTime;
startTime = Date.now();
if (remainingTime > 0) {
if (timerName === "focusTimer") {
startFocusSession(Math.ceil(remainingTime / 1000));
} else if (timerName === "breakTimer") {
startBreak(Math.ceil(remainingTime / 1000));
}
}
}
function resetTimer() {
clearTimeout(currentTimer);
paused = false;
remainingTime = 0;
updateDurations(); // Update durations before resetting
if (timerName === "focusTimer") {
remainingTime = FOCUS_DURATION / 1000;
chrome.runtime.sendMessage({ action: 'updateTimer', time: formatTime(FOCUS_DURATION / 1000) });
} else if (timerName === "breakTimer") {
remainingTime = BREAK_DURATION / 1000;
chrome.runtime.sendMessage({ action: 'updateTimer', time: formatTime(BREAK_DURATION / 1000) });
}
timerName = ""; // Clear the timer name to indicate no active timer
}
function formatTime(seconds) {
let minutes = Math.floor(seconds / 60);
let secs = seconds % 60;
return `${minutes}:${secs < 10 ? '0' : ''}${secs}`;
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'startFocus') {
startFocusSession(request.duration);
sendResponse({ status: 'Focus session started' });
} else if (request.action === 'startBreak') {
startBreak(request.duration);
sendResponse({ status: 'Break started' });
} else if (request.action === 'pause') {
pauseTimer();
sendResponse({ status: 'Timer paused' });
} else if (request.action === 'resume') {
resumeTimer();
sendResponse({ status: 'Timer resumed' });
} else if (request.action === 'reset') {
resetTimer();
sendResponse({ status: 'Timer reset' });
} else if (request.action === 'getTimerStatus') {
let timeLeft = Math.round(remainingTime);
sendResponse({ time: formatTime(timeLeft) });
}
return true; // Keep the message channel open for asynchronous response
});
// Listen for changes in storage
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync' && (changes.focusDuration || changes.breakDuration)) {
updateDurations();
}
});