-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.js
330 lines (289 loc) · 8.82 KB
/
timer.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
var index = 0;
var task;
var state = "pomodoro";
var pause = 1;
var focuTime, shortTime, longTime, cycle;
var countTimer;
var playlist;
const videosContainer = document.getElementById('playlist-videos');
const mode = document.getElementById('current-mode');
const taskSpan = document.getElementById('task');
const cycleSpan = document.getElementById('cycles');
window.onload = async () => {
await chrome.storage.local.get(["focusTime", "shortTime", "longTime", "longTimeCycle"], (res) => {
focuTime = res.focusTime;
shortTime = res.shortTime;
longTime = res.longTime;
cycle = res.longTimeCycle;
countTimer = focuTime * 60;
flipAllCards(countTimer);
})
await chrome.storage.local.get("playlist", (data) => {
if (typeof data.playlist === 'undefined') {
//no music added to playlist
} else {
playlist = data.playlist;
console.log(playlist);
renderVideos();
}
})
await chrome.storage.local.get("index", (data) => {
index = data.index;
});
await chrome.storage.local.get("tasks", (data) => {
task = data.tasks[index];
taskSpan.innerText = task.text;
state = task.state;
mode.innerText = state;
if (state == "shortBreak") {
shortBreak();
} else if (state == "longBreak") {
longBreak();
} else {
focusMode();
}
})
}
window.onunload=function(){
if(windowId!=-1){
chrome.windows.remove(windowId);
}
}
//render videos
const renderVideos = () => {
videosContainer.innerHTML = "";
playlist.forEach((video, index) => {
const outerDiv = document.createElement('div');
outerDiv.classList.add('small-div')
const title = document.createElement('h4');
title.classList.add('side_head')
title.innerText = video.videoTitle;
const vidImg = document.createElement('img');
vidImg.setAttribute("src", `${video.videoThumbnail}`)
vidImg.classList.add('side-img')
const deleteBtn = document.createElement('button');
deleteBtn.innerText = "Delete";
deleteBtn.classList.add('delete-btn')
deleteBtn.addEventListener('click', () => {
deleteVideo(index);
})
outerDiv.append(vidImg);
outerDiv.append(title);
outerDiv.append(deleteBtn);
videosContainer.append(outerDiv);
});
}
//delete Videos
const deleteVideo = (index) => {
if(play==1){
alert("You Can't Delete while Songs are Playing")
}else{
playlist.splice(index, 1);
renderVideos();
saveVideos();
}
}
const saveVideos = () => {
chrome.storage.local.set({ "playlist": playlist });
}
//countdown
setInterval(() => {
if (!pause) {
countTimer = countTimer - 1;
if (countTimer == 0) {
pause = 1;
if (state === "pomodoro") {
task.completedCycle += 1;
saveTasks();
if (task.completedCycle == task.pomodoroCycle) {
taskCompleted();
}
if (task.completedCycle < cycle) {
state = "shortBreak";
shortBreak();
} else {
state = "longBreak";
longBreak();
}
} else {
state = "pomodoro";
focusMode();
}
} else {
flipAllCards(countTimer);
}
}
}, 1000)
//start functionality
const startBtn = document.getElementById('start-btn');
startBtn.addEventListener('click', () => {
pause = 1 - pause;
if (pause) {
startBtn.innerText = "Start";
} else {
startBtn.innerText = "Pause";
}
})
//reset functionality
const resetBtn = document.getElementById('reset-btn');
resetBtn.addEventListener('click', () => {
pause = 1;
if (state == "pomodoro") {
focusMode();
} else if (state == "shortBreak") {
shortBreak();
} else {
longBreak();
}
startBtn.innerText = "Start";
})
//focusmode function
const focusMode = () => {
task.state = "pomodoro";
saveTasks();
console.log(focuTime);
flipAllCards(focuTime * 60);
console.log(focuTime);
countTimer = focuTime * 60;
startBtn.innerText = "Start";
mode.innerText = "Focus";
chrome.storage.local.set({ "state": "focus" });
}
//short Break function
const shortBreak = () => {
task.state = "shortBreak";
saveTasks();
flipAllCards(shortTime * 60);
countTimer = shortTime * 60;
startBtn.innerText = "Start";
mode.innerText = "Short Break";
chrome.notifications.create(
{
title: 'WorkPulse',
message: 'woahh man!! Time for short Break',
iconUrl: './icon.png',
type: 'basic'
}
);
chrome.storage.local.set({ "state": "none" });
}
//long Break function
const longBreak = () => {
task.state = "LongBreak";
saveTasks();
flipAllCards(longTime * 60);
countTimer = longTime * 60;
startBtn.innerText = "Start";
mode.innerText = "Long Break";
chrome.notifications.create(
{
title: 'WorkPulse',
message: 'woahh man!! Time for Long Break',
iconUrl: './icon.png',
type: 'basic'
}
)
chrome.storage.local.set({ "state": "none" });
}
//save Tasks
const saveTasks = () => {
chrome.storage.local.get("tasks", (data) => {
data.tasks[index] = task;
chrome.storage.local.set({ "tasks": data.tasks });
});
}
//task completed function
const taskCompleted = () => {
task.done = true;
saveTasks();
chrome.notifications.create(
{
title: 'WorkPulse',
message: 'Congratulations Task Completed',
iconUrl: './icon.png',
type: 'basic'
}
)
window.location.href = "popup.html";
}
//clock updation in HTML
async function flipAllCards(time) {
const seconds = time % 60
const minutes = Math.floor(time / 60) % 60
const hours = Math.floor(time / 3600)
flip(document.querySelector("[data-hours-tens]"), Math.floor(hours / 10))
flip(document.querySelector("[data-hours-ones]"), hours % 10)
flip(document.querySelector("[data-minutes-tens]"), Math.floor(minutes / 10))
flip(document.querySelector("[data-minutes-ones]"), minutes % 10)
flip(document.querySelector("[data-seconds-tens]"), Math.floor(seconds / 10))
flip(document.querySelector("[data-seconds-ones]"), seconds % 10)
}
function flip(flipCard, newNumber) {
const topHalf = flipCard.querySelector(".top")
const startNumber = parseInt(topHalf.textContent)
if (newNumber === startNumber) return
const bottomHalf = flipCard.querySelector(".bottom")
const topFlip = document.createElement("div")
topFlip.classList.add("top-flip")
const bottomFlip = document.createElement("div")
bottomFlip.classList.add("bottom-flip")
top.textContent = startNumber
bottomHalf.textContent = startNumber
topFlip.textContent = startNumber
bottomFlip.textContent = newNumber
topFlip.addEventListener("animationstart", e => {
topHalf.textContent = newNumber
})
topFlip.addEventListener("animationend", e => {
topFlip.remove()
})
bottomFlip.addEventListener("animationend", e => {
bottomHalf.textContent = newNumber
bottomFlip.remove()
})
flipCard.append(topFlip, bottomFlip)
}
//back button
const backBtn = document.getElementById('back');
backBtn.addEventListener('click', () => {
if(windowId!=-1){
chrome.windows.remove(windowId);
}
window.location.href = "popup.html";
})
//playing song from playlist
var play = 0;
var windowId=-1;
const playBtn = document.getElementById('play-btn');
playBtn.addEventListener('click', async () => {
if (playlist.length === 0) {
alert("no songs added to playlist");
} else {
play = 1 - play;
var video_index = 0;
if (play == 0) {
playBtn.innerText="Play Songs";
if(windowId!=-1){
chrome.windows.remove(windowId);
}
} else {
playBtn.innerText="Pause Songs";
while (play == 1) {
chrome.windows.create({
'url': `https://www.youtube.com/watch?v=${playlist[video_index].videoId}`,
'state': 'minimized'
}, (window) => {
windowId = window.id;
})
await sleep(1000 * 60 * 3);
chrome.windows.remove(windowId);
video_index = video_index + 1;
video_index = video_index % playlist.length;
}
}
}
})
//function for pausing
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}