-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
103 lines (89 loc) · 3.92 KB
/
Copy pathbackground.js
File metadata and controls
103 lines (89 loc) · 3.92 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
let userData = {};
let countdownInterval;
chrome.runtime.onMessage.addListener((message) => {
if(chrome.runtime.lastError){
console.log("Error retrieving chrome storage data");
}
if(message.type === 'startCountdown'){
chrome.storage.local.get(["userData"], (result) => {
userData = result.userData;
const totalSecondsCombined = (userData.hours*3600) + (userData.minutes*60);
const endTime = new Date().setSeconds(new Date().getSeconds() + totalSecondsCombined);
countdownInterval = setInterval( () => { updateCountdown(endTime) }, 1000);
})
}
if(message.type === 'end-early'){
stopCountdown();
}
})
function updateCountdown(endTime){
const remainingSeconds = (endTime - Date.now()) / 1000;
if(remainingSeconds < 1){
stopCountdown();
return; //return so that the code below is not executed after clearing interval
}
const hours = Math.floor(remainingSeconds / 3600);
const minutes = Math.floor((remainingSeconds / 60) % 60)
const seconds = Math.floor(remainingSeconds % 60);
chrome.runtime.sendMessage({type: "update-popup-text", hours: hours, minutes: minutes, seconds: seconds});
}
function stopCountdown(){
clearInterval(countdownInterval);
chrome.storage.local.get(["userData"], (result) => {
result.userData.countdownIsRunning = false;
chrome.storage.local.set({userData: result.userData}, () => {
chrome.runtime.sendMessage({type: "countdown-ended"});
chrome.tabs.query({active: true, currentWindow: true}, (tab) => {
if(tab[0].url){
const URLobj = new URL(tab[0].url);
let parsedURL = URLobj.hostname;
parsedURL = parsedURL.replace(/^www\./, '')
if(result.userData.allowedURLs.includes(tab[0].url)){
return;
}
else if(result.userData.blocklist.includes(parsedURL)){
chrome.tabs.update(tab[0].id, {url: "blocked.html"});
}
}
})
})
})
}
chrome.tabs.onUpdated.addListener((_, changeInfo, tab) => {
if(tab.url && changeInfo.status === 'complete'){
chrome.storage.local.get(["userData"], (result) => {
const currentDate = new Date();
if(result.userData.daysOfWeek.includes(currentDate.getDay())){
const URLobj = new URL(tab.url);
let parsedURL = URLobj.hostname;
parsedURL = parsedURL.replace(/^www\./, '')
if(result.userData.countdownIsRunning === false && result.userData.allowedURLs.includes(tab.url)){
return;
}
else if(result.userData.countdownIsRunning === false && result.userData.blocklist.includes(parsedURL)){
chrome.tabs.update(_, {url: "blocked.html"});
}
}
})
}
})
chrome.tabs.onActivated.addListener(() => {
chrome.tabs.query({active: true, currentWindow: true}, (tab) => {
chrome.storage.local.get(["userData"], (result) => {
const currentDate = new Date();
if(result.userData.daysOfWeek.includes(currentDate.getDay())){
if(tab[0].url){
const URLobj = new URL(tab[0].url);
let parsedURL = URLobj.hostname;
parsedURL = parsedURL.replace(/^www\./, '')
if(result.userData.countdownIsRunning === false && result.userData.allowedURLs.includes(tab[0].url)){
return;
}
else if(result.userData.countdownIsRunning === false && result.userData.blocklist.includes(parsedURL)){
chrome.tabs.update(tab[0].id, {url: "blocked.html"});
}
}
}
})
})
})