-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
189 lines (162 loc) · 5.25 KB
/
script.js
File metadata and controls
189 lines (162 loc) · 5.25 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
// Timer Variables
let workDuration = 25 * 60; // Default 25 min
let breakDuration = 5 * 60; // Default 5 min
let timeLeft = workDuration;
let isWorkSession = true;
let isRunning = false;
let timer;
// Video URLs
let workVideoURL = '';
let breakVideoURL = '';
let useSameVideo = false;
let currentVideoURL = ''; // Tracks the currently loaded video URL
// DOM Elements
const timerDisplay = document.getElementById('timer');
const startBtn = document.getElementById('start');
const resetBtn = document.getElementById('reset');
const loadWorkMusicBtn = document.getElementById('load-work-music');
const loadBreakMusicBtn = document.getElementById('load-break-music');
const sameVideoCheckbox = document.getElementById('same-video');
const workMusicUrlInput = document.getElementById('work-music-url');
const breakMusicUrlInput = document.getElementById('break-music-url');
const musicPlayer = document.getElementById('music-player');
const workInput = document.getElementById('work-duration');
const breakInput = document.getElementById('break-duration');
// Notification Sounds
const break_will_start = new Audio('break_will_start.m4a'); // 5 min before work ends
const break_has_started = new Audio('break_has_started.m4a'); // At break start
const work_will_start = new Audio('work_will_start.m4a'); // 1 min before break ends
const work_has_started = new Audio('work_has_started.m4a'); // At work restart
// Timer Functions
function updateTimerDisplay() {
const minutes = Math.floor(timeLeft / 60).toString().padStart(2, '0');
const seconds = (timeLeft % 60).toString().padStart(2, '0');
timerDisplay.textContent = `${minutes}:${seconds}`;
}
function startTimer() {
if (isRunning) return;
isRunning = true;
// Auto-play the video only if it's not already playing
if (useSameVideo) {
if (currentVideoURL !== workVideoURL && workVideoURL) {
loadMusic(workVideoURL, true);
currentVideoURL = workVideoURL;
}
} else {
if (isWorkSession) {
loadMusic(workVideoURL, true);
currentVideoURL = workVideoURL;
} else {
loadMusic(breakVideoURL, true);
currentVideoURL = breakVideoURL;
}
}
timer = setInterval(() => {
if (
isWorkSession &&
timeLeft === 5 * 60 &&
workDuration > 5 * 60
) {
break_will_start.play();
}
if (
!isWorkSession &&
timeLeft === 60 &&
breakDuration > 60
) {
work_will_start.play();
}
if (timeLeft > 0) {
timeLeft--;
updateTimerDisplay();
} else {
clearInterval(timer);
isRunning = false;
sessionSwitch();
}
}, 1000);
}
function resetTimer() {
clearInterval(timer);
isRunning = false;
isWorkSession = true;
timeLeft = workDuration;
updateTimerDisplay();
loadMusic(workVideoURL); // Reset to original work music
currentVideoURL = workVideoURL;
}
function sessionSwitch() {
if (isWorkSession) {
// Switch to break session
isWorkSession = false;
timeLeft = breakDuration;
break_has_started.play(); // Break starts
if (!useSameVideo && breakVideoURL) {
loadMusic(breakVideoURL, true);
currentVideoURL = breakVideoURL;
}
} else {
// Switch back to work session
isWorkSession = true;
timeLeft = workDuration;
work_has_started.play(); // Work starts
if (!useSameVideo && workVideoURL) {
loadMusic(workVideoURL, true);
currentVideoURL = workVideoURL;
}
}
updateTimerDisplay();
startTimer(); // Automatically start next session
}
// Music Integration
function loadMusic(url, autoplay = false) {
if (url && url.includes('youtube.com')) {
if (currentVideoURL !== url) {
musicPlayer.innerHTML = `
<iframe width="100%" height="200"
src="${url.replace('watch?v=', 'embed/')}${autoplay ? '?autoplay=1' : ''}"
frameborder="0" allow="autoplay; encrypted-media"
allowfullscreen>
</iframe>`;
currentVideoURL = url;
}
} else if (!url) {
console.warn('No YouTube URL provided. Skipping video load.');
}
}
// Set Custom Durations with Decimal Support
function setCustomDurations() {
const workInputValue = parseFloat(workInput.value); // Parse as float
const breakInputValue = parseFloat(breakInput.value); // Parse as float
if (!isNaN(workInputValue) && workInputValue > 0) {
workDuration = Math.round(workInputValue * 60); // Convert to seconds
}
if (!isNaN(breakInputValue) && breakInputValue > 0) {
breakDuration = Math.round(breakInputValue * 60); // Convert to seconds
}
resetTimer();
}
// Event Listeners
startBtn.addEventListener('click', startTimer);
resetBtn.addEventListener('click', resetTimer);
loadWorkMusicBtn.addEventListener('click', () => {
workVideoURL = workMusicUrlInput.value;
loadMusic(workVideoURL);
});
loadBreakMusicBtn.addEventListener('click', () => {
breakVideoURL = breakMusicUrlInput.value;
if (useSameVideo) {
breakVideoURL = workVideoURL;
}
});
sameVideoCheckbox.addEventListener('change', (e) => {
useSameVideo = e.target.checked;
if (useSameVideo) {
breakVideoURL = workVideoURL;
currentVideoURL = workVideoURL; // Prevent reload
}
});
workInput.addEventListener('change', setCustomDurations);
breakInput.addEventListener('change', setCustomDurations);
// Initialize Timer Display
updateTimerDisplay();