public
Last active

Gmail Fluid App Userscript

  • Download Gist
fluid_gmail.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
window.fluid.dockBadge = '';
setTimeout(updateDockBadge, 1000);
setTimeout(updateDockBadge, 3000);
setInterval(updateDockBadge, 5000);
 
function updateDockBadge() {
var newBadge = '';
var res = findInboxAnchorMatchResult();
if (res) {
newBadge = res[1];
}
 
window.fluid.dockBadge = newBadge;
}
 
function findInboxAnchorMatchResult() {
var xpathType = XPathResult.FIRST_ORDERED_NODE_TYPE;
var xpathResult = document.evaluate('//div[@role="navigation"]', document, null, xpathType, null);
var canvas = xpathResult.singleNodeValue;
//console.log('canvas: ' + canvas);
if (!canvas) return null;
 
// get document
var doc = canvas.contentDocument || canvas.ownerDocument;
//console.log('doc: ' + doc);
if (!doc) return null;
 
// loop thru anchor tags
var anchorEls = doc.getElementsByTagName('a');
//console.log('anchors: ' + anchorEls.length);
if (!anchorEls || !anchorEls.length) return null;
 
for (var i = 0; i < anchorEls.length; i++) {
var anchorEl = anchorEls[i];
//console.log('anchorEl: '+ anchorEl);
var res = matchInAnchorEl(anchorEl);
if (res) {
//console.log('res: '+ res);
return res;
}
}
 
return null;
}
 
function matchInAnchorEl(anchorEl) {
var text = '' + anchorEl.innerText;
if (!text.length) return null;
if (-1 == text.indexOf('(')) return null;
 
var regex = /\s*Inbox\s*\((\d+)\)[^\d]*/;
var res = text.match(regex);
if (res && res.length > 1) {
//console.log('res: '+ res);
return res;
} else {
return null;
}
}

I made a fork of this and greatly simplified this. It works for me. https://gist.github.com/kirbysayshi/5356592

This script was failing if I had more than one window open, such as another message. In that case, scripts in both windows would try to set the badge count, resulting in it flickering.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.