-
Notifications
You must be signed in to change notification settings - Fork 2
/
unread-count-for-outlook.js
68 lines (59 loc) · 2.25 KB
/
unread-count-for-outlook.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
window.addEventListener("load", (event) => {
let favicon = new Favico({
bgColor: '#ffffff',
textColor: '#000000',
animation: 'none'
});
function update_unread() {
favicon.reset();
// Try to find the unread count close to <i data-icon-name="Inbox"
let iElements = document.getElementsByTagName('i');
for (let i = 0; i < iElements.length; i++) {
let iElement = iElements[i];
if (isInboxIcon(iElement)) {
let numericValue = findSpanWithNumber(iElement.parentElement.parentElement);
if (numericValue)
favicon.badge(numericValue);
return;
}
}
// If we did not find the icon, fallback to any <span containing a number
let numericValue = findSpanWithNumber(document);
if (numericValue)
favicon.badge(numericValue);
}
function isInboxIcon(iElement) {
if (!iElement.attributes['data-icon-name']) {
return false;
}
let value = iElement.attributes['data-icon-name'].value;
if (typeof value === "string") {
return value.toLowerCase().includes("inbox");
} else {
return false;
}
}
function findSpanWithNumber(element) {
let spanElements = element.getElementsByTagName('span');
for (let i = 0; i < spanElements.length; i++) {
let child = spanElements[i].firstChild;
if (child && child.nodeType === 3) {
let text = child.data.trim();
if (text.length > 0 && !Number.isNaN(Number(text))) {
return Number(text);
}
}
}
}
function deferred_update_unread() {
setTimeout(update_unread, 1000);
}
console.log("Starting unread count for Outlook extension");
// Not sure when the page will be ready after initial loading :(
setTimeout(update_unread, 2000);
// Backup mode if events don't work
setInterval(update_unread, 10000);
// At least we should be notified when a new message arrives
let observer = new MutationObserver(deferred_update_unread);
observer.observe(document.body, {characterData: true, subtree: true});
});