Skip to content

Instantly share code, notes, and snippets.

@lylo
Last active April 14, 2021 08:59
Show Gist options
  • Save lylo/7786717 to your computer and use it in GitHub Desktop.
Save lylo/7786717 to your computer and use it in GitHub Desktop.
Userscript for Fluid App (http://fluidapp.com) to enable dock count and desktop notifications for Fastmail
// Userscript for Fluid App to enable dock badge and desktop notifications for Fastmail.
//
// Author: @lylo
window.fluid.dockBadge = '';
// for tracking when to notify
let previousUnreadCount = 0;
// Main polling
let readyStateCheckInterval = setInterval(function() {
if(document.readyState === "complete") {
setInterval(updateDockBadge, 5000);
clearInterval(readyStateCheckInterval);
}
}, 100);
function updateDockBadge() {
var currentUnreadCount = 0;
var tree = document.getElementsByClassName("v-MailboxSource");
for(i = 0; i < tree.length; ++i) {
_name = tree[i].getElementsByClassName("app-source-name")[0].innerText;
badgeTree = tree[i].getElementsByClassName("v-MailboxSource-badge");
badge = '';
if(badgeTree.length > 0) {
badge = badgeTree[0].innerText;
}
// if you only want to report on the Inbox, change the following line to
// if (badge && _name && _name == "Inbox")
if (badge && _name) {
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