forked from wavebox/wavebox-service-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangouts.js
36 lines (33 loc) · 1.06 KB
/
hangouts.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
(function () {
const getUnreadCount = function () {
try {
const chatFrame = document.querySelector('#hangout-landing-chat iframe').contentWindow.document.body
return chatFrame.querySelectorAll('.ee').length
} catch (ex) {
return 0
}
}
let prev
window.Notification.requestPermission()
setInterval(function () {
const next = getUnreadCount()
if (next !== prev) {
window.wavebox.badge.setCount(next)
if (next === 0) {
window.wavebox.tray.setMessages([])
} else if (next === 1) {
window.wavebox.tray.setMessages([{ text: '1 unread message' }])
} else if (next > 1) {
window.wavebox.tray.setMessages([{ text: next + ' unread messages' }])
}
if (prev !== undefined && next !== undefined && next > prev) {
const diff = next - prev
const notif = new window.Notification('New Hangouts Message', {
body: 'You have ' + diff + ' new message' + (diff > 1 ? 's' : '')
})
notif.onclick = function () {}
}
prev = next
}
}, 1500)
})()