Created
October 6, 2015 12:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for creating this script. It's working for me. :)