-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
47 lines (43 loc) · 1.18 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
chrome.storage.sync.get(['checkedNotifications'], function(storedData) {
var storedCheckedNotifications = (
Object.keys(storedData).length !== 0 &&
storedData.constructor === Object
) ? JSON.parse(storedData.checkedNotifications, false) : [];
chrome.alarms.onAlarm.addListener(function(alarm) {
var checkedNotification = getObjectElement(storedCheckedNotifications, 'id', alarm.name);
var quote = '"'+checkedNotification.quote+'"';
chrome.notifications.create({
type: 'basic',
iconUrl: 'assets/png/icon48.png',
title: checkedNotification.title,
message: quote,
buttons: [
{title: 'Done'}
],
priority: 0
});
});
chrome.runtime.onStartup.addListener(function () {
setAlarms(storedCheckedNotifications);
});
});
function getObjectElement(haystack, key, value) {
for (const element of haystack) {
if ( element[key] === value ) {
return element;
}
}
return false;
}
function setAlarms(checkedNotifications){
chrome.alarms.clearAll();
checkedNotifications.forEach(function(notification){
chrome.alarms.create(
notification.id,
{
when: Date.now() + notification.cycleTime * 60000,
periodInMinutes: notification.cycleTime
}
);
});
}