Last active
August 29, 2015 14:14
-
-
Save chmanie/e03c408245179fd94db2 to your computer and use it in GitHub Desktop.
Userscript for using FastMail in Fluid.app with proper dock badges and (!) Message Center notifications with preview
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
| /* Options */ | |
| // set dock badge refresh interval here | |
| var refreshInterval = 10*1000; // 10 seconds | |
| // on some systems or networks the site takes a long time to load. specify this time here | |
| var firstTimeout = 3*1000; // 3 seconds work fine for me | |
| var count = 0; | |
| window.fluid.dockBadge = ''; | |
| setTimeout(function () { | |
| setDockBadge(true); | |
| }, firstTimeout); | |
| document.onclick = function () { | |
| setTimeout(function() { | |
| setDockBadge(true); | |
| }, 500); | |
| }; | |
| function showNotification() { | |
| var unreadElms = document.querySelectorAll('.is-unread'); | |
| var unread = [].slice.call(unreadElms); | |
| unread = unread.map(function(item) { | |
| return item.id && parseInt(item.id.substr(1)); | |
| }); | |
| var maxId = Math.max.apply(Math, unread); | |
| var from = document.querySelector('#v' + maxId + ' .v-MailboxItem-name span').innerText; | |
| var subject = document.querySelector('#v' + maxId + ' .v-MailboxItem-subject').title; | |
| var body = document.querySelector('#v' + maxId + ' .v-MailboxItem-preview').innerText; | |
| var n = new Notification('New Mail: ' + from, { body: subject + '\n' + body }); | |
| n.onclick = function () { | |
| this.close(); | |
| }; | |
| } | |
| function setDockBadge(supressNotification) { | |
| var elm = document.querySelector('.v-FolderSource-badge'); | |
| var newCount = parseInt(elm && elm.innerText); | |
| if (newCount > count) { | |
| if (!supressNotification) showNotification(); | |
| } | |
| count = newCount; | |
| if (newCount > 0) { | |
| window.fluid.dockBadge = '' + newCount; | |
| } else { | |
| window.fluid.dockBadge = ''; | |
| count = 0; | |
| } | |
| setTimeout(setDockBadge, refreshInterval); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment