-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
74 lines (66 loc) · 2.63 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
/* chrome.storage.sync.get('myArray', function(data) {
data.myArray.length=0;
chrome.storage.sync.set({'myArray':data.myArray}, function(){
alert(data.myArray.length=0);
});
});*/
chrome.runtime.onMessage.addListener(function(request, sender,sendResponse) {
if (request.greeting == "hello")
{
var url = new URL(sender.tab.url);
var domain = url.hostname;
sendResponse({farewell: (domain+"")});
}
else
chrome.tabs.update(sender.tab.id, {url: request.redirect});
});
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
chrome.storage.sync.get('myArray', function(data) {
for (var i=0;i<data.myArray.length;i++){
var url = new URL(tabToUrl[tabId]);
var domain = url.hostname;
if(domain.toString()==data.myArray[i][0]){
console.log(domain.toString());
var d=new Date();
var s="";
var c=3;
var nDate=dateAndTime(d.getFullYear(), d.getMonth(),d.getDate(),d.getHours(),d.getMinutes(),parseInt((data.myArray[i])[1],10),parseInt((data.myArray[i])[2],10));
for(var j=0;j<nDate.length;j++){
if(nDate.charAt(j)!=" "){
s=s+nDate.charAt(j);
}else{
(data.myArray[i])[c]=parseInt(s);
c++;
s="";
}
}
chrome.storage.sync.set({'myArray':data.myArray}, function(){
});
// Block the notifications from the website
var primaryPattern = "https://" + domain.toString() + ":*/*";
var hours = parseInt((data.myArray[i])[1], 10);
var mins = parseInt((data.myArray[i])[2],10);
var notificationsBlockTime = hours * 60 + mins;
chrome.contentSettings["notifications"].set({
'primaryPattern': primaryPattern, 'setting': "block"},
function(details){
chrome.alarms.create(primaryPattern, {'delayInMinutes': notificationsBlockTime});
});
alert("Oops! Now You Will Not Be Able To Open " +tabToUrl[tabId].toString()+" for next "+((parseInt((data.myArray[i])[1],10)*60)+(parseInt((data.myArray[i])[2],10))).toString()+" minutes");
break;
}
}
});
});
var tabToUrl = {};
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
tabToUrl[tabId] = tab.url;
});
function unblockWebsite(primaryPattern) {
chrome.contentSettings["notifications"].set({
'primaryPattern': primaryPattern, 'setting': "allow"});
}
chrome.alarms.onAlarm.addListener(function(alarm) {
var primaryPattern = alarm.name;
unblockWebsite(primaryPattern);
});