-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_worker.js
More file actions
94 lines (93 loc) · 2.95 KB
/
service_worker.js
File metadata and controls
94 lines (93 loc) · 2.95 KB
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
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason=="install"){
chrome.storage.local.set({setting:{min:45,max:75}})
}
});
chrome.runtime.onMessage.addListener(function(req,res,reply){
if(req.msg=="new-scroll-session"){
addNewSession(req.site,req.totalTime)
}else
if(req.msg=="re-assign"){
reAssign();
}
return true;
})
const addNewSession=(site,minutes)=>{
let currentDate=getTodayDate(),newData={};
chrome.storage.local.get(currentDate,(info)=>{
let data={},obj={minutes:0,peroid:0};
if(info && info[currentDate]){
data=info[currentDate];
}
if(data[site]){
obj=data[site];
}
let mint=parseFloat(obj.minutes)+minutes;
if(mint<=0) mint=0;
obj.minutes=mint;
obj.peroid=parseFloat(obj.peroid)+1;
data[site]=obj;
newData[currentDate]=data;
chrome.storage.local.set(newData);
displayBadges(data)
})
}
const getTodayDate=()=>{
let date=new Date(),
day =date.getDate(),
month=date.getMonth()+1,
year = date.getFullYear();
if(day<10){
day='0'+day;
}
if(month<10) {
month='0'+month;
}
return day + '-' + month + '-' + year;
}
const changeBadge=(color,text,title)=>{
chrome.action.setBadgeBackgroundColor({color});
chrome.action.setBadgeText({text});
chrome.action.setTitle({title});
chrome.storage.local.set({ totalMinutes: parseFloat(text)});
}
const displayBadges=(data)=>{
chrome.storage.local.get("setting",(info)=>{
if(info && info.setting){
let min=info.setting.min,
max=info.setting.max;
total=0;
for(const key in data){
if(data.hasOwnProperty(key)){
total+=data[key].minutes;
}
}
total=Math.floor(total);
if(total>min){
if(total<max){
changeBadge("#f5c102",`${total}`,"Alert: You have exceeded "+min+" min of daily scrolling time! Consider a walk outside!");
}else{
changeBadge("#ff4747",`${total}`,"Warning: You have exceeded "+max+" minutes of scrolling time for today");
}
}else{
changeBadge("#90e30e",`${total}`,"Well Done! You are under "+min+" min of scrolling time!");
}
}
})
}
let cd=getTodayDate();
chrome.storage.local.get(cd,(info)=>{
if(!info || !info[cd]){
data=info[cd];
chrome.action.setTitle({title:"Screen Nutrition: Track your scrolling"});
}
});
const reAssign=()=>{
let currentDate=getTodayDate();
chrome.storage.local.get(currentDate,(info)=>{
if(info && info[currentDate]){
let data=info[currentDate];
displayBadges(data)
}
})
}