-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
144 lines (115 loc) · 4.62 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
// set up notification interval
chrome.storage.local.get(['notificationInterval'], function(result) {
let interval;
let selectedNotificationInterval;
console.log('The stored notification interval: ' + result.notificationInterval);
selectedNotificationInterval = parseInt(result.notificationInterval, 10)
if (isNotificationIntervalValid(selectedNotificationInterval)) {
interval = selectedNotificationInterval
} else {
// default
interval = 30
}
chrome.alarms.create('read later notifier', { delayInMinutes : 1, periodInMinutes : interval });
});
// load selected folder name in popup
let selectedFolderName;
chrome.storage.local.get(['selectedFolder'], function(result) {
console.log('The stored folder name: ' + result.selectedFolder);
selectedFolderName = result.selectedFolder
});
// load selected number of notifications at one time
let selectedNumberOfNotifications;
chrome.storage.local.get(['numberOfNotifications'], function(result) {
console.log('The stored number of notifications: ' + result.numberOfNotifications);
selectedNumberOfNotifications = parseInt(result.numberOfNotifications, 10)
});
// set up addListener for notifying read later contents periodically
chrome.alarms.onAlarm.addListener(function(alarm){
// get all bookmark tree
chrome.bookmarks.getTree(function(bookmark){
let folderName;
if (isFolderNameValid(selectedFolderName)) {
folderName = selectedFolderName
} else {
// default
folderName = "read_later"
}
console.log('folder name: ' + folderName)
let notificationsCount;
if (isNumberOfNotificationsValid(selectedNumberOfNotifications)) {
notificationsCount = selectedNumberOfNotifications
console.log('then branch. notificationsCount: '+ notificationsCount)
} else {
// default
notificationsCount = 3
}
console.log('notifications count: ' + notificationsCount)
var root = bookmark[0]['children']
var bookMarkBar = root[0]['children']
// get read later folder from book mark bar
var readLater = bookMarkBar.filter(function(elem, index, array) {
return isChildrenArray(elem) && elem.title == folderName
})
// get random read later contents from readLater
var selectedReadLater = getReadLaterContents(readLater[0]['children'], notificationsCount)
console.log(selectedReadLater)
selectedReadLater.forEach(function(item, index, array) {
var notification = new Notification(item.title, {
body: "",
requireInteraction: true
});
notification.addEventListener("click", function (event) {
notificationClicked(item.url)
notification.close()
} )
})
})
});
function getReadLaterContents(contentsArray, notificationsCount) {
if (notificationsCount == 0) {
return []
}
var selectedContentsArray = []
const contentTotalCount = contentsArray.length
var randomIndexesArray = getRandomIndexes(contentTotalCount, notificationsCount)
randomIndexesArray.forEach( elem => {
selectedContentsArray.push(contentsArray[elem])
})
return selectedContentsArray
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
// get selected number of indexes without duplication
// to avoid showing same contents
function getRandomIndexes(contentTotalCount, notificationsCount) {
var randomIndexesArray = []
for (let index = 0; index < notificationsCount; index++) {
while(true) {
var randomInt = getRandomInt(0, contentTotalCount)
if(!randomIndexesArray.includes(randomInt)) {
randomIndexesArray.push(randomInt)
break
}
}
}
return randomIndexesArray
}
function isChildrenArray(elem) {
return 'children' in elem
}
function notificationClicked(url) {
chrome.tabs.create({ "url": url, active: false });
}
function isFolderNameValid(folderName) {
return folderName != "" | folderName != null || folderName != undefined
}
function isNumberOfNotificationsValid(folderName) {
return folderName != "" | folderName != null || folderName != undefined
}
function isNotificationIntervalValid(notificationInterval) {
return notificationInterval != "" | notificationInterval != null || notificationInterval != undefined
}