-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
112 lines (100 loc) · 3.21 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
let timer = null;
let duration = 0;
let isPaused = false;
const Session = {
Work: 'work',
ShortBreak: 'shortBreak',
LongBreak: 'longBreak',
};
let currentSession = Session.Work;
let workSessionsCompleted = 0;
// chrome.runtime.onInstalled.addListener(async () => {
// for (const cs of chrome.runtime.getManifest().content_scripts) {
// for (const tab of await chrome.tabs.query({url: cs.matches})) {
// chrome.scripting.executeScript({
// target: {tabId: tab.id},
// files: cs.js,
// });
// }
// }
// });
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'startTimer') {
chrome.storage.sync.get(['workDuration', 'shortBreakDuration', 'longBreakDuration'], (data) => {
workDuration = data.workDuration;
shortBreakDuration = data.shortBreakDuration;
longBreakDuration = data.longBreakDuration;
duration = request.duration;
isPaused = false;
startTimer();
})
} else if (request.action === 'pauseTimer') {
isPaused = true;
} else if (request.action === 'resumeTimer') {
isPaused = false;
} else if(request.action === 'resetTimer') {
clearInterval(timer);
duration = 0;
isPaused = false;
currentSession = Session.Work;
chrome.runtime.sendMessage({action: 'updateTimer', duration: formatDuration(duration)});
} else if (request.action === 'getDuration') {
sendResponse({duration: formatDuration(duration)});
}
});
function startTimer() {
if (timer) {
clearInterval(timer);
}
// chrome.storage.sync.get(['workDuration', 'shortBreakDuration', 'longBreakDuration'], (data) => {
// if (data.workDuration) {
// let workDuration = data.workDuration;
// }
// if (data.shortBreakDuration) {
// let shortBreakDuration = data.shortBreakDuration;
// }
// if (data.longBreakDuration) {
// longBreakDuration = data.longBreakDuration;
// }
// });
timer = setInterval(() => {
if (!isPaused) {
duration--;
if (duration <= 0) {
clearInterval(timer);
switch (currentSession) {
case Session.Work:
console.log("work duration: " + workDuration);
console.log("duration " + duration);
workSessionsCompleted++;
if (workSessionsCompleted === 4) {
currentSession = Session.LongBreak;
duration = longBreakDuration;
} else {
currentSession = Session.ShortBreak;
duration = shortBreakDuration;
}
break;
case Session.ShortBreak:
case Session.LongBreak:
currentSession = Session.Work;
duration = workDuration;
break;
}
startTimer();
}
// Store the remaining duration
chrome.storage.sync.set({remainingDuration: duration});
}
// Send a message to the popup to update the display
chrome.runtime.sendMessage({action: 'updateTimer', duration: formatDuration(duration)});
}, 1000);
}
function formatDuration(duration) {
let minutes = Math.floor(duration / 60);
let seconds = duration % 60;
if (seconds < 10) {
seconds = '0' + seconds;
}
return String(minutes) + ':' + String(seconds);
}