Skip to content

Instantly share code, notes, and snippets.

@freekrai
Created December 8, 2015 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freekrai/4c80ee051431db847a78 to your computer and use it in GitHub Desktop.
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
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