Instantly share code, notes, and snippets.

Embed
What would you like to do?
Userscript you can use with Fluid App to add a counter to the dock.
/*
Fluid App Userscript
FastMail web interface
URL pattern: *www.fastmail.com*
*/
// For best results, make sure to turn on desktop notifications
// in fastmail. Otherwise the counter will only update every 90s
// instead of instantly.
(function() {
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
function getFolderCount(name) {
var folderEl = document.getElementsByClassName('v-FolderSource--' + name)[0];
if (!folderEl) return 0;
var badgeEl = folderEl.getElementsByClassName('v-FolderSource-badge')[0];
if (!badgeEl) return 0;
return parseInt(badgeEl.innerText.trim()) || 0;
}
function updateBadge() {
var inboxCount = getFolderCount('inbox');
console.log("Badge = " + inboxCount);
if (inboxCount > 0) {
window.fluid.dockBadge = inboxCount;
} else {
window.fluid.dockBadge = "";
}
}
var debounceUpdateBadge = debounce(updateBadge, 1000);
window.setTimeout(updateBadge, 2000);
window.setTimeout(updateBadge, 5000);
window.setTimeout(updateBadge, 10000);
window.setInterval(updateBadge, 90000);
var oldNotify = window.Notification;
window.Notification = function(title, opt) {
if (window.fluid.dockBadge && window.fluid.dockBadge !== "") {
window.fluid.dockBadge++;
} else {
window.fluid.dockBadge = 1;
}
new oldNotify(title, opt);
}
document.addEventListener('keydown', debounceUpdateBadge);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment