-
Notifications
You must be signed in to change notification settings - Fork 9
/
background.js
144 lines (129 loc) · 4.44 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
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
// background.js
// Timer variables
let timerInterval;
let startTime;
let elapsedTime = 0;
let isTimerRunning = false;
const firstIntervalDuration = 20 * 60 * 1000; // 20 minutes in milliseconds
const secondIntervalDuration = 20 * 1000; // 20 seconds for break
// Start timer
function startTimer() {
chrome.storage.local.get(["startTime", "elapsedTime"], function (result) {
startTime = result.startTime || Date.now();
elapsedTime = result.elapsedTime || 0;
if (elapsedTime > 0) {
startTime = Date.now() - elapsedTime; // Adjust start time by elapsed time
}
timerInterval = setInterval(updateTimer, 1000);
isTimerRunning = true;
chrome.storage.local.set({ isTimerRunning: true });
});
}
// Pause timer
function pauseTimer() {
clearInterval(timerInterval);
isTimerRunning = false;
chrome.storage.local.set({ isTimerRunning: false, elapsedTime: elapsedTime });
}
// Resume timer
function resumeTimer() {
if (!isTimerRunning) {
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(updateTimer, 1000);
isTimerRunning = true;
chrome.storage.local.set({ isTimerRunning: true });
}
}
// Reset timer
function resetTimer() {
clearInterval(timerInterval);
elapsedTime = 0;
isTimerRunning = false;
chrome.storage.local.set({ startTime: null, elapsedTime: 0, isTimerRunning: false });
chrome.runtime.sendMessage({ type: "updateTimer", time: formatTime(firstIntervalDuration) });
}
// Update timer
function updateTimer() {
const currentTime = Date.now();
elapsedTime = currentTime - startTime;
const remainingTime = firstIntervalDuration - elapsedTime;
if (remainingTime <= 0) {
clearInterval(timerInterval);
chrome.runtime.sendMessage({ type: "updateTimer", time: "00:00" });
notifyBreakTime();
startShortTimer();
} else {
chrome.runtime.sendMessage({ type: "updateTimer", time: formatTime(remainingTime) });
}
}
// Format time function
function formatTime(time) {
const minutes = Math.floor(time / 60000);
const seconds = Math.floor((time % 60000) / 1000);
return `${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}
// Function to notify break time
function notifyBreakTime() {
chrome.runtime.sendMessage({ type: "breakTimeNotification"});
}
// Function to start short break timer
function startShortTimer() {
startTime = Date.now();
timerInterval = setInterval(() => {
const elapsedBreakTime = Date.now() - startTime;
const remainingBreakTime = secondIntervalDuration - elapsedBreakTime;
if (remainingBreakTime <= 0) {
clearInterval(timerInterval);
chrome.runtime.sendMessage({ type: "updateTimer", time: formatTime(firstIntervalDuration) });
notifyBreakEnd();
} else {
chrome.runtime.sendMessage({ type: "updateTimer", time: formatTime(remainingBreakTime) });
}
}, 1000);
}
// Function to notify break end
function notifyBreakEnd() {
chrome.notifications.create("breakEndNotification", {
type: "basic",
iconUrl: "images/hourglass.png",
title: "Break time over",
message: "Time to get back to work! The 20-20-20 timer will restart."
});
startTimer();
}
// Listen for messages from popup script
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
switch (message.type) {
case "startTimer":
startTimer();
break;
case "pauseTimer":
pauseTimer();
break;
case "resumeTimer":
resumeTimer();
break;
case "resetTimer":
resetTimer();
break;
case "getTimerState":
chrome.storage.local.get(["isTimerRunning", "elapsedTime"], function (result) {
sendResponse({
isTimerRunning: result.isTimerRunning || false,
elapsedTime: result.elapsedTime || 0
});
});
return true; // asynchronous response
}
});
// Initialization
chrome.runtime.onInstalled.addListener(() => {
resetTimer();
});
// Listen for keyboard shortcuts
chrome.commands.onCommand.addListener((command) => {
if (command === "activate_extension") {
console.log("Extension Activated"); // Log for debugging
chrome.action.openPopup(); // Open the extension popup
}
});