Created
December 8, 2015 19:31
-
-
Save freekrai/4c80ee051431db847a78 to your computer and use it in GitHub Desktop.
Userscript for Fluid App (http://fluidapp.com) to enable dock count and desktop notifications for Google Inbox
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
window.fluid.dockBadge = ''; | |
// for tracking when to notify | |
var previousUnreadCount = 0; | |
// Main polling | |
var readyStateCheckInterval = setInterval(function() { | |
if(document.readyState === "complete") { | |
setInterval(updateDockBadge, 3000); | |
clearInterval(readyStateCheckInterval); | |
} | |
}, 3000); | |
function updateDockBadge() { | |
var currentUnreadCount = 0; | |
var badge = document.getElementsByClassName('qG').length; | |
if (badge) { | |
if(parseInt(badge) > 0) { | |
currentUnreadCount += parseInt(badge); | |
} | |
} | |
if(currentUnreadCount == 0) { | |
window.fluid.dockBadge = ''; | |
previousUnreadCount = 0; | |
} else { | |
// update dock with new count | |
window.fluid.dockBadge = currentUnreadCount; | |
// determine how many new mails there are and notify | |
var unreadMailCount = currentUnreadCount - previousUnreadCount; | |
if(unreadMailCount > 0) { | |
notify(unreadMailCount); | |
previousUnreadCount = currentUnreadCount; | |
} | |
} | |
} | |
// Creates a Notification Center message, if supported | |
function notify(count) { | |
var supportsWebkitNotifications = ('webkitNotifications' in window); | |
if(count > 0 && supportsWebkitNotifications) { | |
if(webkitNotifications.checkPermission() == 0) { | |
body = (count == 1) ? '1 new message' : count + ' new messages'; | |
webkitNotifications.createNotification( | |
null, | |
'You got mail', | |
body | |
).show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment