-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
214 lines (185 loc) · 7.18 KB
/
popup.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
document.addEventListener('DOMContentLoaded', function() {
const chosenMessageText = document.getElementById('chosenMessage');
const startButton = document.getElementById('startButton');
const resetButton = document.getElementById('resetButton');
const countdownInput = document.getElementById('countdownInput');
let timerInterval; // Variable to hold the setInterval ID
let countdownTime; // Variable to hold the remaining countdown time
let isPaused = false; // Variable to track pause state
let productive = ['Fish-tastic job!', 'Keep swimming :)', `You're really fish-tastic!`,
'I sea you working...', 'A true ocean master!', `You're such a starfish!`];
let unproductive = ['Oh, barnacles.',
`I don't sea you working hard.`, `I'm getting flooded...`, 'Your fish are gonna drown!',
`Someone's finally taking a shower...`, 'Make more waves with your productivity'];
function chooseMessage(callback) {
let listURL = JSON.parse(localStorage.getItem("productivity under the sea")) || [];
let num = Math.floor(Math.random() * 6);
console.log(listURL);
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
let currentTabURL = tabs[0].url;
console.log(currentTabURL);
if (listURL.some(url => currentTabURL.includes(url))) {
callback(unproductive[num]);
} else {
callback(productive[num]);
}
});
}
chooseMessage(function(message) {
chosenMessageText.innerText = message;
});
//updates the timer
function updateTime() {
chrome.storage.local.get(["timer"], (res) => {
const totalSeconds = res.timer;
const displaySeconds = totalSeconds % 60;
const displayMinutes = Math.floor((totalSeconds % 3600) / 60);
const displayHours = Math.floor(totalSeconds / 3600);
const time = document.getElementById("timer");
const hours = String(displayHours).padStart(2, "0");
let minutes;
// if (displayMinutes > 0) {
// displayMinutes = String(displayMinutes - 1).padStart(2, "0");
// } else {
minutes = String(displayMinutes).padStart(2, "0");
// }
let seconds = String(displaySeconds).padStart(2, "0");
console.log(hours, minutes, seconds)
time.textContent = `${hours}:${minutes}:${seconds}`;
});
}
//is called once to initialize from countdown input
function setTimer() {
// countdownTime = parseInt(countdownInput.value) * 60;
// const inputTime = countdownTime;
// if (!isNaN(inputTime)) {
// chrome.storage.local.set({ "timer": inputTime });
updateTime();
// }
}
// // Function to update the timer display
// function updateTimer(seconds) {
// const hours = Math.floor(seconds / 3600);
// const minutes = Math.floor((seconds % 3600) / 60);
// const remainingSeconds = seconds % 60;
// const formattedTime = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
// timerDisplay.textContent = formattedTime;
// }
// Function to start the countdown timer
function startCountdown() {
countdownTime = parseInt(countdownInput.value) * 60;
if (isNaN(countdownTime) || countdownTime <= 0) {
alert('Please enter a valid countdown time (greater than 0).');
return;
}
//initializes timer
updateTime(countdownTime);
//gets timer ending time from storage?
timerInterval = localStorage.getItem("productivity under the sea");
// Start the countdown
timerInterval = setInterval(function() {
if (!isPaused) {
countdownTime--;
// Store countdownTime in chrome storage
chrome.storage.sync.set({ countdownTime: countdownTime });
}
if (countdownTime <= 0) {
clearInterval(timerInterval);
}
}, 1000);
timerUpdateInterval = setInterval(function() {
updateTime(countdownTime);
})
// Enable pause button and disable start button
startButton.disabled = true;
pauseButton.disabled = false;
resetButton.disabled = false;
}
// Function to pause the countdown timer
function pauseCountdown() {
clearInterval(timerInterval);
isPaused = true;
startButton.disabled = false;
pauseButton.disabled = true;
resetButton.disabled = false;
chrome.storage.local.set({ isRunning: false });
}
// Function to reset the countdown timer
function resetCountdown() {
clearInterval(timerInterval);
countdownTime = 0;
updateTime(countdownTime);
isPaused = false;
startButton.disabled = false;
pauseButton.disabled = true;
resetButton.disabled = true; // Disable the reset button after resetting the timer
// Remove countdownTime from chrome storage
chrome.storage.sync.remove('countdownTime');
}
//helper function to store prev time for resume
// Function to start the countdown timer after resuming
function resumeCountdownHelper(remainingTime) {
countdownTime = remainingTime;
if (isNaN(countdownTime) || countdownTime <= 0) {
alert('Invalid countdown time.');
return;
}
updateTime(countdownTime);
// Start the countdown
timerInterval = setInterval(function() {
if (!isPaused) {
countdownTime--;
updateTime(countdownTime);
}
if (countdownTime <= 0) {
clearInterval(timerInterval);
// Send a message to the content script
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: 'startWaterAnimation'});
});
}
}, 1000);
// Function to resume the countdown timer
function resumeCountdown(remainingTime) {
isPaused = false;
resumeCountdownHelper(remainingTime);
}
}
updateTime();
setInterval(updateTime, 1000);
//resets the buttons back to default
resetButton.addEventListener("click", () => {
chrome.storage.local.set({
timer: 0,
isRunning: false,
}, () => {
startButton.textContent = "Start Timer"
})
})
//switches between start and pause and sets timer
startButton.addEventListener("click", () => {
chrome.storage.local.get(["timer", "isRunning"], (res) => {
if (isNaN(res.timer) || res.timer <= 0) {
countdownTime = parseInt(countdownInput.value) * 60;
const inputTime = countdownTime;
if (!isNaN(inputTime)) {
chrome.storage.local.set({ "timer": inputTime });
}
}
setTimer();
chrome.storage.local.set({
isRunning: !res.isRunning,
}, () => {
startButton.textContent = !res.isRunning ? "Pause Timer" : "Start Timer"
console.log("Starting timer! with time: " + countdownInput.value);
countdownTime = parseInt(countdownInput.value) * 60;
//starts the water animation w the initial countdown time input
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: 'startWaterAnimation', parameter: countdownTime});
});
})
})
})
pauseButton.addEventListener('click', pauseCountdown);
resetButton.addEventListener('click', resetCountdown);
});