-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
137 lines (121 loc) · 3.86 KB
/
background.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
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
134
135
136
let page_history_stats = {};
let current_page_start_time = null;
let current_page = null;
let seconds_remaining = null;
let is_started = false;
var counter = null;
function format_url(urls){
let formatted_urls = [];
for(let i=0; i<urls.length; i++){
formatted_urls.push("*://" + urls[i] + "/*");
}
console.log(formatted_urls);
return formatted_urls;
}
function block_websites() {
return {cancel: true};
}
function reset_globals() {
//TODO: clear intervals
page_history_stats = {};
current_page_start_time = null;
current_page = null;
seconds_remaining = null;
counter = null;
}
function countdown() {
seconds_remaining -= 1;
console.log(seconds_remaining);
if (seconds_remaining <= 0) {
clearInterval(counter);
}
}
function updateCurrentPage(){
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
// and use that tab to fill in out title and url
console.log("tabs: ", tabs);
if(tabs.length > 0){
var tab = tabs[0];
if(tab.hasOwnProperty('url')){
current_page = tab.url.split("/")[2];
console.log('updated current_page to ' + current_page);
}
}
});
}
function updateCurrentTime(){
current_page_start_time = Date.now();
}
function recordPages(){
let elapsed_time = Math.round((Date.now() - current_page_start_time) / 1000);
console.log('current_page: ' + current_page);
console.log('elapsed_time: ' + elapsed_time);
if (page_history_stats.hasOwnProperty(current_page)) {
page_history_stats[current_page] += elapsed_time;
} else {
page_history_stats[current_page] = elapsed_time;
}
updateCurrentTime();
updateCurrentPage();
console.log(page_history_stats);
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.task === 'start' && seconds_remaining === null) {
is_started = true;
chrome.storage.local.get('blocked_sites', (obj) => {
chrome.webRequest.onBeforeRequest.addListener(
block_websites,
{urls: format_url(obj.blocked_sites)},
["blocking"]
);
});
chrome.tabs.onActivated.addListener(
recordPages
);
chrome.tabs.onUpdated.addListener(
recordPages
// NOTE: we choose multiple triggers over filtering
// for changeInfo.status == 'complete'
);
seconds_remaining = 60;
// TODO: sync this from main.js
counter = setInterval(countdown, 1000);
updateCurrentPage();
updateCurrentTime();
sendResponse(
{'seconds_remaining': seconds_remaining}
);
} else if (request.task === 'stop') {
chrome.webRequest.onBeforeRequest.removeListener(block_websites);
} else if (request.task === 'update_blocked_sites' && seconds_remaining !== null){
chrome.webRequest.onBeforeRequest.removeListener(block_websites);
chrome.storage.local.get('blocked_sites', (obj) => {
chrome.webRequest.onBeforeRequest.addListener(
block_websites,
{urls: format_url(obj.blocked_sites)},
["blocking"]
);
});
} else if (request.task == 'update_timer') {
sendResponse(
{'is_started': is_started,
'seconds_remaining': seconds_remaining}
)
}
}
);
chrome.runtime.onInstalled.addListener(
function() {
chrome.storage.local.set({'blocked_sites': ['www.youtube.com',
'www.netflix.com',
'www.reddit.com']},
function() {
console.log("set blocked sites successfully");
}
);
}
);