-
Notifications
You must be signed in to change notification settings - Fork 9
/
popup.js
161 lines (138 loc) · 5.06 KB
/
popup.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const timerElement = document.getElementById("timer");
const pauseButton = document.getElementById("pause");
const startButton = document.getElementById("start");
const resumeButton = document.getElementById("resume");
const resetButton = document.getElementById("reset");
const themeToggle = document.getElementById("theme-toggle");
let custom_message="It's time to take a break. Look at something 20 feet away for 20 seconds.";
chrome.storage.sync.get('customMessage', (data)=> {
if (data.customMessage) {
custom_message=data.customMessage;
}
});
// Schedule Tab elements
const startTimeInput = document.getElementById("startTime");
const endTimeInput = document.getElementById("endTime");
const saveScheduleButton = document.getElementById("saveSchedule");
// Timer state variables
let isTimerPaused = false;
let isTimerRunning = false;
// Switch between timer and schedule tabs
document.querySelectorAll('.tab-button').forEach(button => {
button.addEventListener('click', function() {
document.querySelectorAll('.tab-content').forEach(tab => {
tab.style.display = 'none';
});
document.getElementById(this.dataset.target).style.display = 'block';
document.querySelectorAll('.tab-button').forEach(btn => {
btn.classList.remove('active');
});
this.classList.add('active');
});
});
// Function to update button states
function updateButtonStates() {
startButton.disabled = isTimerRunning;
pauseButton.disabled = !isTimerRunning || isTimerPaused;
resumeButton.disabled = !isTimerPaused || isTimerRunning;
resetButton.disabled = false;
}
// Function to start the timer
startButton.addEventListener("click", function() {
chrome.runtime.sendMessage({ type: "startTimer" });
isTimerRunning = true;
isTimerPaused = false;
updateButtonStates();
});
// Function to pause the timer
pauseButton.addEventListener("click", function() {
chrome.runtime.sendMessage({ type: "pauseTimer" });
isTimerPaused = true;
isTimerRunning = false;
updateButtonStates();
});
// Function to resume the timer
resumeButton.addEventListener("click", function() {
chrome.runtime.sendMessage({ type: "resumeTimer" });
isTimerPaused = false;
isTimerRunning = true;
updateButtonStates();
});
// Function to reset the timer
resetButton.addEventListener("click", function() {
chrome.runtime.sendMessage({ type: "resetTimer" });
isTimerRunning = false;
isTimerPaused = false;
updateButtonStates();
});
// Listen for timer updates from background script
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.type === "updateTimer") {
timerElement.textContent = message.time;
}
else if(message.type === "breakTimeNotification"){
chrome.notifications.create("breakTimeNotification", {
type: "basic",
iconUrl: "images/hourglass.png",
title: "Take a break!",
message: custom_message
});
}
});
// Function to save the schedule
saveScheduleButton.addEventListener("click", function() {
const startTime = startTimeInput.value;
const endTime = endTimeInput.value;
if (startTime && endTime) {
chrome.storage.local.set({ startTime, endTime }, function() {
alert("Schedule saved!");
});
} else {
alert("Please set both a start and end time.");
}
});
// Theme toggle
themeToggle.addEventListener("change", function() {
if (this.checked) {
document.body.classList.add("dark-theme");
localStorage.setItem("darkTheme", "enabled");
} else {
document.body.classList.remove("dark-theme");
localStorage.setItem("darkTheme", "disabled");
}
});
// Function to detect and apply system theme
function detectSystemTheme() {
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
if (localStorage.getItem("darkTheme") === null) {
if (prefersDarkScheme) {
document.body.classList.add("dark-theme");
themeToggle.checked = true;
localStorage.setItem("darkTheme", "enabled");
} else {
document.body.classList.remove("dark-theme");
themeToggle.checked = false;
localStorage.setItem("darkTheme", "disabled");
}
} else if (localStorage.getItem("darkTheme") === "enabled") {
document.body.classList.add("dark-theme");
themeToggle.checked = true;
} else {
document.body.classList.remove("dark-theme");
}
}
// Load initial theme, timer state, and saved schedule times
window.addEventListener("load", function() {
detectSystemTheme();
chrome.runtime.sendMessage({ type: "getTimerState" }, function(response) {
isTimerRunning = response.isTimerRunning;
isTimerPaused = !isTimerRunning && response.elapsedTime > 0;
updateButtonStates();
});
// Load saved schedule times
chrome.storage.local.get(["startTime", "endTime"], function(result) {
if (result.startTime) startTimeInput.value = result.startTime;
if (result.endTime) endTimeInput.value = result.endTime;
});
});
console.log("Popup script loaded");