-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
130 lines (96 loc) · 3.79 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//Initializing the array to store data
var data = [];
//Event listener that is called when the start button is pressed
chrome.runtime.onMessage.addListener((message,sender,sendResponse) =>{
//Clearring data array
data = [];
//Storing values in data including
// workTimeLeft, breakTimeLeft, and numOfCycles
data.push(message.workTimeLeft);
data.push(message.breakTimeLeft);
data.push(message.numOfCycles);
//Logging the data array to the console
console.log(data);
console.log("This data is: " + typeof(data[0]));
//Starting the timer
startWorkCountdown();
})
//Function that logs the time left for user set worktime
function startWorkCountdown() {
console.log( "work Time");
//Temp Counter for desired work time
var workCounter = data[0];
//Call the SetIcon function to change the icon
setIcon("work");
//Interval that runs every second
const interval = setInterval(() => {
//Logs how much time is left + cycles left
console.log("Work Time Left: " + workCounter + "Cycles left:" + data[2]);
//Changes the badge text to the time left
setBadgeText(workCounter);
//If the counter reaches 0, clear the interval and log break time
//And start the break countdown
if (workCounter == 0 ) {
clearInterval(interval);
startBreakCountdown();
}
//decrease the temp value
workCounter -=1;
}, 1000);
}
//Function that logs the time left for user set breaktime
function startBreakCountdown() {
console.log("Break Time");
//Temp counter for desired break time
var breakCounter = data[1];
//Call the setIcon function to change the icon
setIcon("break");
//If cycle counter is greater than 0
//Run the breakdown counter
if (data[2] > 0) {
//Interval that runs every second
const interval = setInterval(() => {
//Logging where the breakConter is at
console.log("Break Time Left: " + breakCounter + "Cycles left:" + data[2]);
//Changes the badge text to the time left
setBadgeText(breakCounter);
//If the breakCounter reaches 0, clear the interval and start the work counter
if (breakCounter == 0) {
clearInterval(interval);
startWorkCountdown();
}
//decrease the temp value
breakCounter -= 1;
}, 1000);
data[2] -= 1;
//If the number of cycles is 0, log that the cycles are complete
//Set icon back to original timer icon
} else if (data[2] == 0) {
console.log("Cycles are complete");
setIcon("timer");
}
}
//Sets chrome extension icon
//Based upon the ID
function setIcon(iconId){
if(iconId == "work"){
chrome.action.setIcon({ path:"/images/breakIcon.png" })
} else if(iconId == "break"){
chrome.action.setIcon({ path: "/images/workIcon.png" })
} else{
chrome.action.setIcon({ path: "/images/timer.png" })
chrome.action.setBadgeText({text: ""});
}
}
//Sets the badge text
function setBadgeText(timeLeft){
//If the time left is greater than 60,
//Divide the time left by 60 and
//add a : to the end of the time left
//This is to display the time in minutes and seconds instead of seconds only
if(timeLeft > 60){
timeLeft = Math.floor(timeLeft/60) + ":" + (timeLeft % 60);
}
chrome.action.setBadgeText({text: timeLeft.toString()});
}
console.log("background.js is running");