-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
133 lines (116 loc) · 2.93 KB
/
background.js
File metadata and controls
133 lines (116 loc) · 2.93 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
132
133
const DEFAULT_STUDYLENGTH = 3600
const DEFAULT_BREAKLENGTH = 600
var favicon = []
var pages = []
var startTime = {}
var storedTime = {}
var instances = {}
var idToUrl = {}
var studyLength = DEFAULT_STUDYLENGTH
var breakLength = DEFAULT_BREAKLENGTH
var studyTimer = new CountDownTimer(studyLength)
var breakTimer = new CountDownTimer(breakLength)
//CountDownTimer
function CountDownTimer(duration, granularity) {
this.duration = duration;
this.granularity = granularity || 1000;
this.tickFtns = [];
this.running = false;
}
CountDownTimer.prototype.start = function() {
if (this.running) {
return;
}
this.running = true;
var start = Date.now(),
that = this,
diff, obj;
(function timer() {
diff = that.duration - (((Date.now() - start) / 1000) | 0);
if (diff > 0) {
setTimeout(timer, that.granularity);
} else {
diff = 0;
that.running = false;
}
obj = CountDownTimer.parse(diff);
that.tickFtns.forEach(function(ftn) {
ftn.call(this, obj.minutes, obj.seconds);
}, that);
}());
};
CountDownTimer.prototype.onTick = function(ftn) {
if (typeof ftn === 'function') {
this.tickFtns.push(ftn);
}
return this;
};
CountDownTimer.prototype.expired = function() {
return !this.running;
};
CountDownTimer.parse = function(seconds) {
return {
'minutes': (seconds / 60) | 0,
'seconds': (seconds % 60) | 0
};
};
//website monitor
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.url != undefined) {
if (tabId in idToUrl) {
closePage(idToUrl[tabId])
}
let hn = new URL(changeInfo.url).hostname
openPage(hn)
idToUrl[tab.id] = hn
}
})
chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
if (tabId in idToUrl) {
closePage(idToUrl[tabId])
}
})
function openPage(url) {
if (!(pages.includes(url))) {
favicon.push(`www.google.com/s2/favicons?domain=${url}`)
if(url != 'newtab') {
pages.push(url)
}
storedTime[url] = 0
instances[url] = 0
console.log(favicon)
}
if (!(url in startTime)) {
startTime[url] = Date.now()
}
instances[url] += 1
}
function closePage(url) {
if (!(url in instances) || (instances[url] == 0)) {
return
}
if (instances[url] == 1) {
storedTime[url] += Date.now() - startTime[url]
delete startTime[url]
}
instances[url] -= 1
}
function removeHistory() {
for (websites in pages) {
pages.pop()
}
}
//study timer
studyTimer.onTick(function () {
if (this.expired()) {
alert('Take a study break!')
breakTimer.start()
}
})
breakTimer.onTick(function() {
if (this.expired()) {
alert('Break time is up!')
studyTimer.start()
}
})
studyTimer.start()