-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbackground.js
More file actions
131 lines (125 loc) · 3.51 KB
/
background.js
File metadata and controls
131 lines (125 loc) · 3.51 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
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
function isValidURL(givenURL){
if(givenURL){
if(givenURL.includes(".")){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
function secondsToString(seconds,compressed=false){
let hours = parseInt(seconds/3600);
seconds = seconds%3600;
let minutes= parseInt(seconds/60);
seconds = seconds%60;
let timeString = "";
if(hours){
timeString += hours + " hrs ";
}
if(minutes){
timeString += minutes + " min ";
}
if(seconds){
timeString += seconds+ " sec ";
}
if(!compressed){
return timeString;
}
else{
if(hours){
return(`${hours}h`);
}
if(minutes){
return(`${minutes}m`);
}
if(seconds){
return(`${seconds}s`);
}
}
};
function getDateString(nDate){
let nDateDate=nDate.getDate();
let nDateMonth=nDate.getMonth()+1;
let nDateYear=nDate.getFullYear();
if(nDateDate<10){nDateDate="0"+nDateDate;};
if(nDateMonth<10){nDateMonth="0"+nDateMonth;};
let presentDate = nDateYear+"-"+nDateMonth+"-"+nDateDate;
return presentDate;
}
function getDomain(tablink){
if(tablink){
let url = tablink[0].url;
return url.split("/")[2];
}
else{
return null;
}
};
function updateTime(){
chrome.tabs.query({"active":true,"lastFocusedWindow": true},function(activeTab){
let domain = getDomain(activeTab);
if(isValidURL(domain)){
let today = new Date();
let presentDate = getDateString(today);
let myObj = {};
myObj[presentDate]={};
myObj[presentDate][domain] = "";
let timeSoFar = 0;
chrome.storage.local.get(presentDate,function(storedObject){
if(storedObject[presentDate]){
if(storedObject[presentDate][domain]){
timeSoFar = storedObject[presentDate][domain]+1;
storedObject[presentDate][domain] = timeSoFar;
chrome.storage.local.set(storedObject,function(){
console.log("Set "+domain+" at "+storedObject[presentDate][domain]);
chrome.browserAction.setBadgeText({'text':secondsToString(timeSoFar,true)});
});
}
else{
timeSoFar++;
storedObject[presentDate][domain] = timeSoFar;
chrome.storage.local.set(storedObject,function(){
console.log("Set "+domain+" at "+storedObject[presentDate][domain]);
chrome.browserAction.setBadgeText({'text':secondsToString(timeSoFar,true)});
})
}
}
else{
timeSoFar++;
storedObject[presentDate] = {};
storedObject[presentDate][domain] = timeSoFar;
chrome.storage.local.set(storedObject,function(){
console.log("Set "+domain+" at "+storedObject[presentDate][domain]);
chrome.browserAction.setBadgeText({'text':secondsToString(timeSoFar,true)});
})
}
});
}
else{
chrome.browserAction.setBadgeText({'text':''});
}
});
// console.log(timeSoFar);
};
var intervalID;
intervalID = setInterval(updateTime,1000);
setInterval(checkFocus,500)
function checkFocus(){
chrome.windows.getCurrent(function(window){
if(window.focused){
if(!intervalID){
intervalID = setInterval(updateTime,1000);
}
}
else{
if(intervalID){
clearInterval(intervalID);
intervalID=null;
}
}
});
}