-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnew-mail-notification.js
101 lines (78 loc) · 3 KB
/
new-mail-notification.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
setTimeout(() => {
const target = $("[title=Inbox]").first().next().next()[0];
const unreadObserver = new MutationObserver(() => {
updateBadge(target.textContent);
});
const newNotificationObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
Array.prototype.forEach.call(mutation.addedNodes, (node) => {
if (node.getAttribute("ispopup") === "1") {
const element = $(node);
if (harvestNotifications(element)) {
attachPopupObserver(element.children().children()[0]);
}
}
});
}
});
});
function harvestNotifications(node) {
const root = $(node).find(".headerMenuDropShadow");
let info = [];
info = root.find("> button > span")
.map((index, element) => {
return element.textContent;
}).toArray();
if (info.length) {
info.shift();
showNotification(info);
}
root.find("[aria-label=Reminders] > div > div > div").each((index, element) => {
info = $(element).find("div > span > span")
.map((index, element) => {
return element.textContent;
}).toArray();
if (info.length) {
info.push($(element).find("div > span > div > div:not([style*='display: none']) > span")
.map((index, element) => {
return element.textContent;
}).toArray().join(" "));
}
showNotification(info);
});
return !!root.length;
}
function attachPopupObserver(node) {
const popupMutationObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
Array.prototype.forEach.call(mutation.addedNodes, (node) => {
harvestNotifications(node);
});
});
});
popupMutationObserver.observe(node, { childList: true });
}
function showNotification(info) {
new Notification(info[0], {
icon: "http://i.imgur.com/lJ4LbVp.png",
body: `Subject: ${info[1]}\n${info[2]}`
});
}
function updateBadge(count) {
document.title = document.title.replace(/^\(\d+\)/g, "");
if (count) {
document.title = `(${count}) ${document.title}`;
}
}
const popups = $("body > [ispopup=1]");
popups.each((index, node) => {
const element = $(node);
if (harvestNotifications(element)) {
attachPopupObserver(element.children().children()[0]);
}
});
updateBadge(target.textContent);
unreadObserver.observe(target, { subtree: true, characterData: true });
newNotificationObserver.observe(document.body, { childList: true });
}, 3000);