forked from GodOfZap/Pomodoro-Timer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
191 lines (162 loc) · 5.67 KB
/
Copy pathpopup.js
File metadata and controls
191 lines (162 loc) · 5.67 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class PomodoroTimer {
constructor() {
this.settings = {
focusTime: 25,
breakTime: 5,
longBreakTime: 15,
soundEnabled: true
};
this.pomodoroCount = 0;
this.mode = 'focus'; // focus, break, longBreak
this.loadElements();
this.loadSettings();
this.addEventListeners();
this.reset();
}
loadElements() {
this.timerDisplay = document.querySelector('.timer');
this.startButton = document.querySelector('.start');
this.resetButton = document.querySelector('.reset');
this.progressBar = document.querySelector('.progress');
this.modeDisplay = document.querySelector('.mode');
this.notificationSound = document.getElementById('notification-sound');
// Settings elements
this.focusInput = document.querySelector('.focus-time');
this.breakInput = document.querySelector('.break-time');
this.longBreakInput = document.querySelector('.long-break-time');
this.soundToggle = document.querySelector('.sound-toggle');
}
loadSettings() {
chrome.storage.local.get(this.settings, (result) => {
this.settings = result;
this.updateSettingsDisplay();
});
}
updateSettingsDisplay() {
this.focusInput.value = this.settings.focusTime;
this.breakInput.value = this.settings.breakTime;
this.longBreakInput.value = this.settings.longBreakTime;
this.soundToggle.checked = this.settings.soundEnabled;
}
addEventListeners() {
this.startButton.addEventListener('click', () => this.toggleTimer());
this.resetButton.addEventListener('click', () => this.reset());
// Settings listeners
this.focusInput.addEventListener('change', () => this.updateSetting('focusTime'));
this.breakInput.addEventListener('change', () => this.updateSetting('breakTime'));
this.longBreakInput.addEventListener('change', () => this.updateSetting('longBreakTime'));
this.soundToggle.addEventListener('change', () => this.updateSetting('soundEnabled'));
// Listen for messages from background
chrome.runtime.onMessage.addListener((message) => {
if (message.action === 'start') {
this.start();
} else if (message.action === 'reset') {
this.reset();
}
});
}
updateSetting(setting) {
const elements = {
focusTime: this.focusInput,
breakTime: this.breakInput,
longBreakTime: this.longBreakInput,
soundEnabled: this.soundToggle
};
const value = setting === 'soundEnabled' ?
elements[setting].checked :
parseInt(elements[setting].value);
this.settings[setting] = value;
chrome.storage.local.set({ [setting]: value });
if (this.mode === 'focus' && setting === 'focusTime' ||
this.mode === 'break' && setting === 'breakTime' ||
this.mode === 'longBreak' && setting === 'longBreakTime') {
this.reset();
}
}
toggleTimer() {
if (this.isRunning) {
this.pause();
} else {
this.start();
}
}
start() {
this.isRunning = true;
this.startButton.textContent = 'Pause';
this.startButton.classList.replace('start', 'pause');
this.timerInterval = setInterval(() => {
this.timeLeft--;
this.updateDisplay();
if (this.timeLeft <= 0) {
this.complete();
}
}, 1000);
}
pause() {
this.isRunning = false;
this.startButton.textContent = 'Start';
this.startButton.classList.replace('pause', 'start');
clearInterval(this.timerInterval);
}
reset() {
this.pause();
this.setTimeForCurrentMode();
this.updateDisplay();
}
setTimeForCurrentMode() {
if (this.mode === 'focus') {
this.timeLeft = this.settings.focusTime * 60;
this.modeDisplay.textContent = 'Focus Time';
} else if (this.mode === 'break') {
this.timeLeft = this.settings.breakTime * 60;
this.modeDisplay.textContent = 'Break Time';
} else {
this.timeLeft = this.settings.longBreakTime * 60;
this.modeDisplay.textContent = 'Long Break';
}
this.initialTime = this.timeLeft;
}
complete() {
this.pause();
this.playNotification();
if (this.mode === 'focus') {
this.pomodoroCount++;
if (this.pomodoroCount % 4 === 0) {
this.mode = 'longBreak';
} else {
this.mode = 'break';
}
} else {
this.mode = 'focus';
}
this.reset();
this.showNotification();
}
updateDisplay() {
const minutes = Math.floor(this.timeLeft / 60);
const seconds = this.timeLeft % 60;
this.timerDisplay.textContent =
`${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
const progress = (this.timeLeft / this.initialTime) * 100;
this.progressBar.style.width = `${progress}%`;
}
playNotification() {
if (this.settings.soundEnabled) {
this.notificationSound.play();
}
}
showNotification() {
const message = this.mode === 'focus' ?
'Time to focus!' :
'Time for a break!';
chrome.notifications.create({
type: 'basic',
iconUrl: 'icon.png',
title: 'Pomodoro Timer',
message: message
});
}
}
document.addEventListener('DOMContentLoaded', () => {
const timer = new PomodoroTimer();
});