Skip to content

Instantly share code, notes, and snippets.

@michaellopez
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save michaellopez/10117149 to your computer and use it in GitHub Desktop.

Select an option

Save michaellopez/10117149 to your computer and use it in GitHub Desktop.
Fluidapp Gmail badge + notifications
/**
* Description: Updates the badge to the unread count.
* Scrapes the inbox for new mail every 5 seconds and displays a notification for each new message.
* Does not support new messages in a conversation. I.e. 1 row in the inbox = 1 notification.
* Only tested on OS X Mountain Lion. This will break when Gmail changes their DOM!
*
* Patterns:
* *gmail.com*
* *mail.google.com*
* *google.com*mail*
*/
function notify(title, msg) {
window.webkitNotifications.createNotification('', title, msg).show();
}
var handledMessages = [];
var count = 0;
function getNewMessages () {
var messages = [], i, n, span, from;
var tr = document.querySelectorAll('div.ae4 table tr.zE');
for(i = 0, n = tr.length; i < n; ++i) {
span = tr[i].querySelector('span[email]');
messages.push({
id: tr[i].id,
from: span.getAttribute('name') + ' <' + span.getAttribute('email') + '>',
title: tr[i].querySelector('td[role=link] span b').textContent,
body: tr[i].querySelector('td[role=link] span.y2').textContent
});
}
return messages;
}
var firstRun = true;
setInterval(checkNewMail, 5000);
function checkNewMail () {
try {
var i, n, messages = getNewMessages();
for (i = 0, n = messages.length; i < n; ++i) {
if (handledMessages.indexOf(messages[i].id) < 0) {
handledMessages.push(messages[i].id);
if (!firstRun) {
notify(messages[i].title, messages[i].from + "\n" + messages[i].body);
}
}
}
firstRun = false;
} catch (e) {
console.log(e);
}
}
// https://gist.github.com/jakebellacera/4772194
window.fluid.dockBadge = '';
setTimeout(updateDockBadge, 3000);
setInterval(updateDockBadge, 2000);
function updateDockBadge() {
var inboxLink = document.querySelector('a[title^="Inbox"]');
var regex = /\s*Inbox\s*\((\d+)\)[^\d]*/;
var res = inboxLink.title.match(regex);
if (res && res.length > 1) {
var newBadge = res[1];
window.fluid.dockBadge = newBadge;
} else {
regex = /^Inbox$/;
if(regex.test(inboxLink.title)){
window.fluid.dockBadge = '';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment