Last active
September 17, 2020 13:16
-
-
Save chroder/8aec68ab898a48a10218 to your computer and use it in GitHub Desktop.
Userscript you can use with Fluid App to add a counter to the dock.
This file contains 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
/* | |
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
Are you still using fluid? Mine stopped showing the badge icon and I can't figure out why. The code properly sets the
window.fluid.dockBadge
property, but the badge isn't shown.