-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
54 lines (47 loc) · 1.56 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
// var websites = {
// "facebook.com" : [
// [['2:00','8:00'], ['11:00','12:00']],
// [['3:00','4:00'], ['11:00','12:00']],
// ]
// ,
// "twitter.com" : [
// [['2:00','4:00'], ['11:00','12:00']],
// [['3:00','4:00'], ['11:00','12:00']]
// ]
// };
var websites = {};
var websiteNames = [];
chrome.storage.sync.get("info",function(obj){
if (obj.info)
websites = obj.info;
websiteNames = [...Object.getOwnPropertyNames(websites)].map((url) => {
return urlMaker(url);
});
} );
let urlMaker = url => {return `*://*.${url}/*`};
var hostGetter = url => {
let fullhost = new URL(url).origin;
return fullhost.substring(fullhost.substring(0, fullhost.lastIndexOf('.')).lastIndexOf('.') + 1);
};
var info = JSON.parse(localStorage.getItem('info'));
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
let date = new Date();
if (websites[hostGetter(details.url)] === undefined) return;
for(let val of websites[hostGetter(details.url)][date.getDay()]){
let startTime = parseInt(val[0].split(':').join(''));
let endTime = parseInt(val[1].split(':').join(''));
let actualTime = parseInt(date.toTimeString().split(" ")[0].split(":")[0] + date.toTimeString().split(" ")[0].split(":")[1]);
if ((actualTime >= startTime) && (actualTime < endTime)){
chrome.tabs.update( {url : "blocked.html"});
return {
cancel : true
};
}
}
return {
};
},
{urls: websiteNames},
["blocking"]
);