-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
190 lines (164 loc) · 7.77 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var reminderArray = [];
var expiredArray = [];
//run a refresh every minute to check if a reminder has expired
setInterval(function() {
reminderArray=[];
expiredArray=[];
chrome.storage.local.get(null, function (items) {
for(var i=0;i<items.uniqKey.length;i++){//for each reminder push it into the reminder array
reminderArray.push(items.uniqKey[i]);
}
if(items.uniqKey2 != undefined){
for (var i = 0; i < items.uniqKey2.length; i++) {//for each expired reminder push it into the expired array
expiredArray.push(items.uniqKey2[i]);
}
}
//now sort the array
var min;
var tDiff;
for(var i=0; i<reminderArray.length; i++){
min=i;
for(var j=i+1;j<reminderArray.length;j++){
tDiff = (Date.parse(reminderArray[min].expireDate)-Date.parse(reminderArray[j].expireDate));
if(tDiff>0){
//find the minimum value in each cycle through the array
min=j;
}
}
//swap the minimum value with the starting indexed value
var tmp = reminderArray[min];
reminderArray[min] = reminderArray[i];
reminderArray[i] = tmp;
}
//If a reminder's expireTime passes by, send it to the expired Array
for(var i=0; i<reminderArray.length;i++){
if((Date.parse(Date())-Date.parse(reminderArray[i].expireDate)) >= 0){
//cut it out of the reminderArray and put in expiredArray and re-adjust counter
var removedVal = reminderArray.splice(i,1);
expiredArray.push(removedVal[0]);
//if a reminder has expired, alert the content tab with the reminder's message
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {expiredReminder: removedVal[0].msg}, function(response) {});
});
i--;
}
}
//and update the storage for both
chrome.storage.local.set({uniqKey2: expiredArray}, function () {
});
chrome.storage.local.set({uniqKey: reminderArray}, function () {
});
//send all of the reminders to the popup
chrome.runtime.sendMessage({allReminders: reminderArray, expiredReminders: expiredArray, cmd: "sendAll"}, function () {
//send all of the reminders
});
});
}, 60000);
//receive a message from the popup
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.cmd == "handshake" && request.msg == "handshake") {
//if the message is a handshake, then get all of the data and send it to the popup
reminderArray=[];
expiredArray=[];
chrome.storage.local.get(null, function (items) {
for(var i=0;i<items.uniqKey.length;i++){//for each reminder push it into the reminder array
reminderArray.push(items.uniqKey[i]);
}
if(items.uniqKey2 != undefined){
for (var i = 0; i < items.uniqKey2.length; i++) {//for each expired reminder push it into the expired array
expiredArray.push(items.uniqKey2[i]);
}
}
//now sort the array
var min;
var tDiff;
for(var i=0; i<reminderArray.length; i++){
min=i;
for(var j=i+1;j<reminderArray.length;j++){
tDiff = (Date.parse(reminderArray[min].expireDate)-Date.parse(reminderArray[j].expireDate));
if(tDiff>0){
//find the minimum value in each cycle through the array
min=j;
}
}
//swap the minimum value with the starting indexed value
var tmp = reminderArray[min];
reminderArray[min] = reminderArray[i];
reminderArray[i] = tmp;
}
//If a reminder's expireTime passes by, send it to the expired Array
for(var i=0; i<reminderArray.length;i++){
if((Date.parse(Date())-Date.parse(reminderArray[i].expireDate)) >= 0){
//cut it out of the reminderArray and put in expiredArray and re-adjust counter
var removedVal = reminderArray.splice(i,1);
expiredArray.push(removedVal[0]);
i--;
}
}
//and update the storage for both
chrome.storage.local.set({uniqKey2: expiredArray}, function () {
});
chrome.storage.local.set({uniqKey: reminderArray}, function () {
});
//send all of the reminders to the popup
chrome.runtime.sendMessage({allReminders: reminderArray, expiredReminders: expiredArray, cmd: "sendAll"}, function () {
//send all of the reminders
});
});
}
if(request.cmd == "delete"){
//if the message is a delete, take the newList from the message and replace the old array with the new one
reminderArray = request.newList;
//replace the array in storage with the new one
chrome.storage.local.set({uniqKey: reminderArray}, function () {
});
}
if(request.cmd == "deleteExp"){
//if the message is a delete, take the newList from the message and replace the old array with the new one
expiredArray = request.newExpiredList;
//replace the array in storage with the new one
chrome.storage.local.set({uniqKey2: expiredArray}, function () {
});
}
if(request.cmd == "dismissAll"){
//if the message is a delete, take the newList from the message and replace the old array with the new one
expiredArray = [];
//replace the array in storage with the new one
chrome.storage.local.set({uniqKey2: expiredArray}, function () {
});
}
if(request.cmd == "normal"){
//if the message is just a new reminder, push it onto the array and sync the array with the key
reminderArray.push(request);
//sort the array based on expireddates
var min;
var tDiff;
for(var i=0; i<reminderArray.length; i++){
min=i;
for(var j=i+1;j<reminderArray.length;j++){
tDiff = (Date.parse(reminderArray[min].expireDate)-Date.parse(reminderArray[j].expireDate));
if(tDiff>0){
//find the minimum value in each cycle through the array
min=j;
}
}
//swap the minimum value with the starting indexed value
var tmp = reminderArray[min];
reminderArray[min] = reminderArray[i];
reminderArray[i] = tmp;
}
chrome.storage.local.set({uniqKey: reminderArray}, function () {
});
reminderArray=[];
chrome.storage.local.get(null, function (items) {
for(var i=0;i<items.uniqKey.length;i++){//for each reminder push it into the reminder array
reminderArray.push(items.uniqKey[i]);
}
chrome.runtime.sendMessage({allReminders: items,cmd: "refresh"}, function () {
//send all of the reminders and tell the popup to refresh
});
});
}
}
);