forked from kshitij10496/Read-Later
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
130 lines (105 loc) · 3.21 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
//window.onload = function()
chrome.windows.onCreated.addListener(function()
{
var tzoffset = (new Date()).getTimezoneOffset() * 60000;
var date2 = (Date.now() - tzoffset)
var comments="You were scheduled to visit this link";
var i = 0;
var oldlink = JSON.parse(localStorage.getItem(i));
while(oldlink!=null)
{
var date = oldlink.date;
var time = oldlink.time;
if(oldlink.epoch < date2 && oldlink.value==0)
{
console.log(oldlink);
var title = oldlink.title;
var url = oldlink.url;
var options = {
type: "image",
title: "Read-Later",
message: title,
contextMessage :comments,
iconUrl: "icon.png",
buttons: [{
title: "Not Now"
}],
imageUrl: "icon.png",
requireInteraction: true,
isClickable : true
};
chrome.notifications.create(options);
chrome.notifications.onClicked.addListener(function(){
window.open(url);
});
chrome.notifications.onButtonClicked.addListener(function(){
window.open("settings.html"); //needs to be replaced by url of site...
});
localStorage.removeItem(i);
var newlink = {"count":i, "url": url ,"title":title,"time":time,"date":date,"comments":comments,"value":1,"epoch":-1};
localStorage.setItem(i, JSON.stringify(newlink));
}
i+=1;
oldlink = JSON.parse(localStorage.getItem(i));
}
});
chrome.alarms.onAlarm.addListener(function(e)
{
if(e.name== "notification_delay")
{
var tzoffset = (new Date()).getTimezoneOffset() * 60000;
var date2 = (new Date(Date.now() - tzoffset)).toISOString().substr(0, 16).replace('T', ' ');
var comments="You are scheduled to visit this link now";
var i = 0;
var oldlink = JSON.parse(localStorage.getItem(i));
while(oldlink!=null )
{
//console.log(oldlink);
var date = oldlink.date;
var time = oldlink.time;
var combo = date+" "+time;
if(combo == date2 && oldlink.value==0 )
{
alert("Yo");
var title = oldlink.title;
var url = oldlink.url;
if(oldlink.comments!=="undefined")
{
if(oldlink.comments.length!==0)
{
comments=oldlink.comments;
}1
}
var options = {
type: "image",
title: "Read-Later",
message: title,
contextMessage :comments,
buttons: [{
title: "Not Now"
}],
iconUrl: "icon.png",
imageUrl: "icon.png",
requireInteraction: true,
isClickable : true
};
chrome.notifications.create(options);
chrome.notifications.onClicked.addListener(function(){
window.open(url);
});
chrome.notifications.onButtonClicked.addListener(function(){
window.open("settings.html"); //needs to be replaced by url of site...
});
localStorage.removeItem(i);
var newlink = {"count":i, "url": url ,"title":title,"time":time,"date":date,"comments":comments,"value":1,"epoch":-1};
localStorage.setItem(i, JSON.stringify(newlink));
break;
}
else
{
i+=1;
oldlink = JSON.parse(localStorage.getItem(i));
}
}
}
});