-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
99 lines (96 loc) · 3.48 KB
/
script.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
currentdate = new Date();
let oneJan = new Date(currentdate.getFullYear(),0,1);
let numberOfDays = Math.floor((currentdate - oneJan) / (24 * 60 * 60 * 1000));
let currWeekResult = Math.ceil(( currentdate.getDay() + 1 + numberOfDays) / 7);
let weekOver = (key) => {
return new Promise((resolve,reject) => {
try{
chrome.storage.local.get(key, function(value){
resolve(value);
})
}
catch(ex){
reject("Error occurred: "+ex);
}
});
}
let getLocalStorageValue = (key) => {
return new Promise((resolve, reject) => {
try {
chrome.storage.local.get(key, function (value) {
resolve(value);
})
}
catch (ex) {
reject("Unexpected error occurred: "+ex);
}
});
}
function starter(){
let starts = document.querySelector("#start");
let child = document.createElement("h2");
child.setAttribute("id","timer-func");
child.innerText = " s";
child.style = "color:#fff";
starts.append(child);
}
weekOver("currWeek")
.then((value)=>{
let currWeekStorage;
if(value.currWeek!=undefined)
currWeekStorage = JSON.parse(value.currWeek);
else{
currWeekStorage=currWeekResult;
}
console.log(currWeekStorage);
if(currWeekResult!=currWeekStorage)
chrome.storage.local.set({'arrWeek':JSON.stringify([0,0,0,0,0,0,0])},function(){
});
let storageArr = undefined;
getLocalStorageValue("arrWeek")
.then((value)=>{
storageArr = value;
if(storageArr.arrWeek!=undefined)
storageArr = JSON.parse(storageArr.arrWeek);
else{
storageArr.arrWeek = [0,0,0,0,0,0,0];
storageArr = storageArr.arrWeek;
}
if(storageArr==undefined)
storageArr = [0,0,0,0,0,0,0];
let today = new Date().getDay();
let todayCount = 0;
todayCount = parseInt(storageArr[today]);
starter();
setInterval( () =>{
let child = document.querySelector("#timer-func");
let hours, min, seconds;
if(window.closed) {
clearInterval(timer);
}
else{
todayCount++;
hours = Math.floor(todayCount/3600);
min = Math.floor((todayCount - hours*3600)/60);
seconds = Math.floor(todayCount - (min*60+hours*3600));
}
let result = " ";
if(hours>0)
result = hours + " Hr " + min+" min "+seconds+" s ";
else if(min > 0)
result = min+" min "+seconds+" s ";
else
result = seconds+" s ";
child.innerHTML = result;
}, 1000);
window.addEventListener('beforeunload', function () {
storageArr[today]=todayCount;
chrome.storage.local.set({'arrWeek':JSON.stringify(storageArr)},function(){
});
});
});
window.addEventListener('beforeunload', function () {
chrome.storage.local.set({'currWeek':JSON.stringify(currWeekResult)},function(){
});
});
})