/
content.js
46 lines (42 loc) · 1.01 KB
/
content.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
(() => {
let unreadCount;
function getUnreadCount(doc) {
if (!doc) {
return -1;
}
const fullcountElement = doc.querySelector('fullcount');
if (!fullcountElement) {
return -1;
}
const count = parseInt(fullcountElement.textContent);
if (isNaN(count)) {
return -1;
}
return count;
}
function getAtomFeed() {
return new Promise((resolve) => {
const x = new XMLHttpRequest();
x.open('GET', 'https://mail.google.com/mail/feed/atom?_=' + new Date().getTime(), true);
x.setRequestHeader('Cache-Control', 'no-cache');
x.onreadystatechange = function () {
if (x.readyState == 4 && x.status == 200) {
resolve(x.responseXML);
}
};
x.send(null);
});
}
async function updateBadgeIcon() {
const feed = await getAtomFeed();
const newUnreadCount = getUnreadCount(feed);
if (newUnreadCount < 0) {
return;
}
if (newUnreadCount !== unreadCount) {
unreadCount = newUnreadCount
navigator.setAppBadge(unreadCount);
}
}
setInterval(updateBadgeIcon, 1000);
})();