Skip to content

Instantly share code, notes, and snippets.

@djui
Created October 6, 2015 12:26
Show Gist options
  • Save djui/7a7dfbfa98cab7805026 to your computer and use it in GitHub Desktop.
Save djui/7a7dfbfa98cab7805026 to your computer and use it in GitHub Desktop.
User script for Google Inbox Fluid App to update the Badge and send notifications on new Emails
var gUnreadMails = new Set()
setInterval(checkUnread, 3000)
function checkUnread() {
updateBadge()
notify()
}
function updateBadge() {
var count = document.querySelectorAll('div.qG').length
window.fluid.dockBadge = (count >= 1) ? count : ''
}
function notify() {
var unreadSubjects = document.querySelectorAll('div.bg.qG > span')
var unreadAuthors = document.querySelectorAll('div.hY.lq > div.rw > span')
var unreadBodies = document.querySelectorAll('div.g6 > span')
Array.prototype.slice.call(unreadSubjects)
.map(function(v, i) {
var subject = v.textContent
var author = unreadAuthors[i].textContent
var body = unreadBodies[i].textContent
return {author: author, subject: subject, body: body}
})
.forEach(function(mail) {
var hash = mail.author + mail.subject + mail.body
if (!gUnreadMails.has(hash)) {
gUnreadMails.add(hash)
sendNotification(mail.author, {body: mail.subject})
}
})
}
function sendNotification(msg, opts) {
if (Notification.permission === 'granted') {
var notification = new Notification(msg, opts);
} else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (permission === 'granted') {
sendNotification(msg, opts)
}
})
}
}
@fatihturan
Copy link

Thank you for creating this script. It's working for me. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment