-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
64 lines (49 loc) · 1.74 KB
/
Copy pathbackground.js
File metadata and controls
64 lines (49 loc) · 1.74 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
const default_conf = {
habitxIsEnabled: true,
habitxBlockCountdownSeconds : 10,
habitxInteruptAfterMinutes : 5,
habitxReenableAfterMinutes : 5,
habitxBlockedSites : [
{ url : 'youtube.com'},
{ url : 'reddit.com'},
{ url : 'twitter.com'}
],
habitxLastDisabledAt : null
}
async function reEnabler() {
const timeout = 1000;
chrome.storage.sync.get(['habitxLastDisabledAt','habitxReenableAfterMinutes'], (data) => {
if(!data.habitxLastDisabledAt){
setTimeout(()=>reEnabler(), timeout);
return;
}
const timeElapsed = Math.floor( data.habitxLastDisabledAt / 1000 ) + ( data.habitxReenableAfterMinutes * 60 );
const now = Math.floor(Date.now() / 1000);
if(now > timeElapsed){
console.log("Re-enabling now...");
chrome.storage.sync.set({habitxIsEnabled: true, habitxLastDisabledAt:null },()=>{
setTimeout(()=>reEnabler(),timeout);
});
} else {
setTimeout(()=>reEnabler(),timeout);
}
});
}
// Installing HabitX
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install"){
console.log("Installing Habitx....");
chrome.storage.sync.set(default_conf);
} else if (details.reason == "update"){
console.log("Updating HabitX...");
chrome.storage.sync.get(Object.keys(default_conf), (data) => {
let _obj = {};
for(const key of Object.keys(default_conf)) {
_obj[key] = data[key] ? data[key] : default_conf[key];
}
console.log(_obj);
chrome.storage.sync.set(_obj);
});
}
});
reEnabler();