Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added background service worker #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions countdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
chrome.alarms.create({delayInMinutes: 1/60, periodInMinutes: 1/60});
chrome.storage.sync.set({"minutes": 25, "seconds": 60, "pause": true, "countDownTimer": "25:00", "pbutton": "pomodoroBtn"},function(){
if(!chrome.runtime.error){
console.log("Initialized");
}
})

let minutes
let seconds
let pause
let array = ["minutes","seconds","pause","countdownTimer","pbutton"];

chrome.alarms.onAlarm.addListener(() => countdownBG());

function countdownBG() {

chrome.storage.sync.get(array,function(value){
if(!chrome.runtime.error){
console.log(value);

if(value.minutes)
minutes = value.minutes;
else
minutes = 25;

if(value.seconds)
seconds = value.seconds;
else
seconds = 60;

pause = (value.pause !== undefined) ? value.pause : true;
countdownCalc();
}
});
}

// background countdown function
function countdownCalc() {
// return if countdown is paused
if(pause) return

// set minutes and seconds
let currentMins = minutes - 1
seconds--
let currentTimer = (currentMins < 10 ? "0" : "") + currentMins.toString() + ':' + (seconds < 10 ? "0" : "") + String(seconds)

chrome.storage.sync.set({"seconds":seconds,"countdownTimer":currentTimer},function(){
if(!chrome.runtime.error){
console.log("started");
}
})
// count down every second, when a minute is up, countdown one minute
// when time reaches 0:00, reset
if(seconds > 0) {
return;
} else if(currentMins > 0){
seconds = 60
minutes--
chrome.storage.sync.set({"seconds":seconds,"minutes":minutes},function(){
if(!chrome.runtime.error){
console.log("started");
}
})
} else if(currentMins === 0) {
chrome.storage.sync.set({"pause": true},function(){
if(!chrome.runtime.error){
console.log("paused");
}
})
}
}
8 changes: 6 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"128": "logo.png"
},
"permissions": [
"storage"
]
"storage",
"alarms"
],
"background": {
"service_worker": "countdown.js"
}
}
130 changes: 69 additions & 61 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,64 @@ const selectedTask = document.getElementById('selected-task')
const audio = document.getElementById('audio')

let tasks = []
let minutes
let seconds
let pause
let minutes
let seconds
let pause
let pomodoro = "pomodoro"
let pomodorosCompleted = 0
let selectedTaskElement

let array = ["minutes","seconds","pause","countdownTimer","pbutton"];
chrome.storage.sync.get(array,function(value){
if(!chrome.runtime.error){
console.log(value);

if(value.minutes)
minutes = value.minutes;
else
minutes = 25;
main();

function main() {
chrome.storage.sync.get(array,function(value){
if(!chrome.runtime.error){
console.log(value);

if(value.minutes)
minutes = value.minutes;
else
minutes = 25;

if(value.seconds)
seconds = value.seconds;
else
seconds = 60;

if(value.countdownTimer)
countdownTimer.innerHTML = value.countdownTimer;
else
countdownTimer.innerHTML = "25:00";
if(value.countdownTimer)
countdownTimer.innerHTML = value.countdownTimer;
else
countdownTimer.innerHTML = "25:00";

if((value.pause) && (value.countdownTimer != "25:00")){
pause = value.pause;
startBtn.innerHTML = "start"
}
else if((!value.pause) && (value.countdownTimer != "25:00")){
pause = value.pause;
startBtn.innerHTML = "Pause"
countdown()
}
else
pause = true;

if (value.pbutton){
if (value.pbutton == "shortBreakBtn"){
shortBreakBtn.classList.add('selected');
if((value.pause) && (value.countdownTimer != "25:00")){
pause = value.pause;
startBtn.innerHTML = "start"
countdown()
}
else if (value.pbutton == "longBreakBtn"){
longBreakBtn.classList.add('selected');
else if((!value.pause) && (value.countdownTimer != "25:00")){
pause = value.pause;
startBtn.innerHTML = "Pause"
countdown()
}
else {
pomodoroBtn.classList.add('selected');
else
pause = true;

if (value.pbutton){
if (value.pbutton == "shortBreakBtn"){
shortBreakBtn.classList.add('selected');
}
else if (value.pbutton == "longBreakBtn"){
longBreakBtn.classList.add('selected');
}
else {
pomodoroBtn.classList.add('selected');
}
}
else
pomodoroBtn.classList.add('selected');
}
else
pomodoroBtn.classList.add('selected');
}
});
});
}


// event listener for pomodoro buttons
document.addEventListener('click', e => {
Expand Down Expand Up @@ -238,7 +242,7 @@ document.addEventListener('click', e => {
const listItem = e.target.closest('.list-item')
listItem.remove()

// find the id of the task to remove the task object from the array
// find the id of the task to remove the task object from the array
const taskId = listItem.dataset.taskId
tasks = tasks.filter(task => task.id !== taskId )

Expand Down Expand Up @@ -285,42 +289,32 @@ function addTask(task) {

// countdown function
function countdown() {
// return if countdown is paused
if(pause) return

// set minutes and seconds
let currentMins = minutes - 1
seconds--
let currentTimer = (currentMins < 10 ? "0" : "") + currentMins.toString() + ':' + (seconds < 10 ? "0" : "") + String(seconds)
countdownTimer.innerHTML = currentTimer

chrome.storage.sync.set({"seconds":seconds,"countdownTimer":currentTimer},function(){
if(!chrome.runtime.error){
console.log("started");
}
})
if(!pause) {
// set minutes and seconds
seconds--
countdownTimer.innerHTML = currentTimer
}
// count down every second, when a minute is up, countdown one minute
// when time reaches 0:00, reset
if(seconds > 0) {
setTimeout(countdown, 1000);
setTimeout(main, 1000);
} else if(currentMins > 0){
seconds = 60
minutes--
chrome.storage.sync.set({"seconds":seconds,"minutes":minutes},function(){
if(!chrome.runtime.error){
console.log("started");
}
})
countdown();
main();
} else if(currentMins === 0) {
audio.play()
reset()
reset()
}
}

// reset function when countdown ends
function reset() {
// set to start the next round
// set to start the next round
startBtn.innerHTML = "Start"
pause = true

Expand Down Expand Up @@ -360,8 +354,22 @@ function reset() {
const current = tasks.find(task => task.id === selectedTaskId)
current.completedPomodoros++
const pomodoroCount = selectedTaskElement.querySelector('.pomodoro-count')
pomodoroCount.innerHTML = current.completedPomodoros.toString() + '/' + current.totalPomodoros
pomodoroCount.innerHTML = current.completedPomodoros.toString() + '/' + current.totalPomodoros
}

// TODO add option to start next round automatically

let dict = {
"minutes": minutes,
"pause": pause,
"seconds": seconds,
"countdownTimer":"25:00",
"pbutton": "pomodoroBtn"
}

chrome.storage.sync.set(dict,function(){
if(!chrome.runtime.error){
console.log("paused");
}
})
}