-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
132 lines (107 loc) · 5.68 KB
/
index.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
(function () {
const CHECK_INTERVAL = 5000;
// @todo abstract
function scrapeForUnreadObjects(itemSelector, propsToSelectors) {
const reminderNodes = Array
.from(document.querySelectorAll(itemSelector));
return reminderNodes
.map(x => Object.keys(propsToSelectors)
.reduce((data, prop) => {
data[prop] = x.querySelector(propsToSelectors[prop]).textContent;
return data;
}, {}))
.map(value => ({
hash : JSON.stringify(value),
value
}));
}
function getUnreadEmailsCount(){
return parseInt(document
.querySelector('[title="Inbox"]')
.parentElement
.querySelector("[id*='ucount']")
.textContent
.replace(/[^0-9]/g, ''), 10);
}
// @todo name should be key so there cannot be dupes
const notifiers = [
{
name : 'Unread Emails',
icon : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAD9GAAA/RgHkW2BAAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAWhQTFRF////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAHAAAGBgAGBgAGBQAFBQAFBQAFBQAFBQAFBQAFBQAFBAAEBAAEBAAEBAAEBAAEBAAEBAAEAwADAwADAwADAwADAwADAwADAwADAwADAwADAgAFAgAFAgAFAgAFAgAFAg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lTh0GxQDTFvDrZvg6ENAdvVevwTJdBJAc/hucLpPc+i0wnCwEuOjJjI6R3ivQc5EPOHIV30n0+lIHrqNcwGaTfvkYac8OTZt6wJItdwEb6acbbEtZgPvJkvu5Xr/7gMn7NOBmoPCYG+huBBi4SQHincW2USPNKtAZF0EiTlDXxLQ+14EzIqyqRkeJkb7bQV1FMXOzyAnjKSgm7zY5YbwC6P6Pq+7dCwB/5ZdtX/VXpapH0h+LEXyi90RG4wAAAABJRU5ErkJggg==',
before : () => notifyMe(`You have ${getUnreadEmailsCount()} new messages!`),
itemSelector : 'div._lvv_w:not(._lvv_y)',
propsToSelectors : {
from : '.lvHighlightFromClass',
subject : '.lvHighlightSubjectClass',
note : '[role="note"]'
},
formatter : (data) => [data.subject, `From: ${data.from}\n${data.note}`]
},
{
name : 'Upcoming Calendar Events',
icon : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAH5QTFRF////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA32SJpAAAACl0Uk5TAAMECQsRFBceIC4/R0hPUlRXWGFohYiMjZKcqKy2uMzQ2d3e3+Dk8P6BWj66AAABE0lEQVRYw+2W3RLBMBCFD4pUVSnqvxQtff8XRDd2zFiGxAxjcq5OcrbfRXcnCcBSRUnatiCqtdUFhRLzQXlVVwZ0uWAg5jHnoQwIuSD+LKC/SittOF+notZcsKGNVb8CZKWxsgqQl7PRRXPep/WdZlww1+tcA0KzfxB+GtDjPJABARf0REBjsiCNazKgNtYFk4YIeFsOcAswlgP8I2CXvKjdA0D66gimDwFeTFLokImAiFwHSofeE4DPR1ZC5gAcyCV85PkO8OOAuk/y0CRzfooock14Oqw/AVhPYlu/BAIMySyBJbkhAh22XRt/HGDdRutBsh5l18a/AFhfbZaTaH29uzfSFwF7c8C+AkyPpt8fp8AJlJenGMZL6tIAAAAASUVORK5CYII=',
itemSelector : '.o365cs-notifications-reminders-listPanel > div',
propsToSelectors : {
title : '.o365cs-notifications-reminders-title',
timeToStart : '.o365cs-notifications-reminders-timeToStartUnit',
timeDuration : '.o365cs-notifications-reminders-timeDuration'
},
formatter : (data) => [data.title, `Event at ${data.timeDuration}`]
}
];
function runNotifiers(oldHashes = {}) {
return notifiers.reduce((acc, notifier) => {
if (typeof notifier.formatter !== 'function') {
throw new TypeError('scraper requires a formatter fn!');
}
oldHashes[notifier.name] = oldHashes[notifier.name] || new Set();
const unreadObjects = scrapeForUnreadObjects(notifier.itemSelector, notifier.propsToSelectors);
const newObjects = unreadObjects
.filter(x => !oldHashes[notifier.name].has(x.hash));
acc[notifier.name] = new Set(unreadObjects.map(x => x.hash));
if(newObjects.length) {
if(typeof notifier.before === 'function') {
notifier.before();
}
newObjects
.map(x => notifier.formatter(x.value))
.forEach(([title, body]) => notifyMe(
title,
{
body,
icon : notifier.icon
}
));
}
return acc;
}, {});
}
function alertNewUnread(oldStateHashes){
if (window.blockNotifications) return false;
const newHashes = runNotifiers(oldStateHashes);
setTimeout(
alertNewUnread,
CHECK_INTERVAL,
newHashes
);
}
alertNewUnread();
function createNotification(title, _options) {
const options = _options || {};
const n = new Notification(title, options);
n.onclick = function() {
window.focus(); this.cancel();
};
return n;
}
function notifyMe(title, options) {
if (!window.Notification) {
return false;
}
Notification.permission === "granted" ?
createNotification(title, options) :
Notification
.requestPermission()
.then(() => notifyMe(title, options));
}
})();