MurkBRA / Gmail Favicon Alerts 3

// ==UserScript==
// @name           Gmail Favicon Alerts 3
// @description    Alerts you to the status of your Gmail Inbox through distinct Favicons.
// @version        3.14
// @date           2013-05-26
// @author         Peter Wooley
// @namespace      http://peterwooley.com
// @icon           data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAACRlBMVEUAAAAAV7IAWbgAWrUAXL0AYMEAY8oAZ9EAatYAa9gAbdgAb9wAcNcAcNoAcd0AcuAAdN4AdeEAdtsAduUAeOIAeeQAeekAfOcAfe0AfukAf/AAge0AgfEAg/QAhO8AhfIAhfQAiPEAiPYAivgAjPoAjvwHjvcKd9sQguQQhecSddYTkPUVfdsViOgWj/IblPUciuscjegfkO8ggNYjl/Qmf9QmiNkmlvInmfMpj9sqid0rktsrlewtm+cvk+UxnewynfE0jdM1o/E3pPI6mt87jcs/kMxEnd9KqOlQre9WsO9Xmc1bq+hgse9rmr9vtu1ynr50q9h0uO51t+15pMJ+oLJ+ueiCpLWIu+KPxO2Rxe+SqrWUxuiVudOVyOqXyeyYx+ifsLmgq62hx92jy+enrq2qsbCq1OysxtCurKKu2O6u2PCvsauw0uOxrqOxrqSysKWy1ue0saa1s6i1uLS2tKi4taq5tqy6uKy8ua69urC+vLG/2ujBvrLCv7TC0tjFwrbG2OHJxbnLyLvL0c7MyLvM2t/Oyr7O4+zPzL7QzL/R2NXR4ObSzsHTz8TU0cTV1czW0cPY1Mba1sjb3tnc5OXc6vHd2cre2szg28zg6Org6ezh3c3h4tvi39Hk38/k39Dl4dHm4tTn5NXn5Nno49Po49To5Nbo6+bq5tjr6Nrr7urs59rs6Nvt6d3u6+Du7N/v7ODv8/Hw7N/w7eHx7uTy8OXy8ejz9Oz08eb18uj29Or39Oz49ev49e369/D6+PH8+vOx8hPUAAAAAXRSTlMAQObYZgAAA4tJREFUSMftlU1vVVUYhZ+1z2lvWwjSDzG0fFijCRg+agEJQRJ/gQOHwMSRExOG/gR/hiOJQiQiJo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tz/eN9ZNs2kVg+7cETw9ZhPavhyY5+18piCcRgV4m0iZiXhjov/t////kHcJEkM9W3SMwAAAAASUVORK5CYII=
// @include        https://mail.google.com/mail*
// @include        http://mail.google.com/mail*
// @include        https://mail.google.com/mail*
// @include        http://mail.google.com/a*
// @include        https://mail.google.com/a*
// @grant          GM_getValue
// @grant          GM_setValue
// @grant          GM_registerMenuCommand
// ==/UserScript==

if(typeof GM_getValue === "undefined") {
	function GM_getValue(name, fallback) {
		return fallback;
	}
}

// Register GM Commands and Methods
if(typeof GM_registerMenuCommand !== "undefined") {
	GM_registerMenuCommand( "Gmail Favicon Alerts > Chat Alerts On", function() { setChat(true) } );
	GM_registerMenuCommand( "Gmail Favicon Alerts > Chat Alerts Off", function() { setChat(false) } );
	GM_registerMenuCommand( "Gmail Favicon Alerts > Unread Count On", function() { setUnreadCountDisplay(true) } );
	GM_registerMenuCommand( "Gmail Favicon Alerts > Unread Count Off", function() { setUnreadCountDisplay(false) } );
	function setChat(val) { GM_setValue('chatEnabled', val) };
	function setUnreadCountDisplay(val) { GM_setValue('unreadCountDisplay', val) };
}

var gfia_instance;
var gfia_chat = GM_getValue('chatEnabled', true);

if(!window.frameElement) {
	new GmailFavIconAlerts();
  console.log("Making an instance.");
}

function GmailFavIconAlerts() {
	var self = this;
	this.construct = function() {				
		this.chat = this.getChat();
		this.chatting = false;
		this.head = window.document.getElementsByTagName('head')[0];
		this.title = this.head.getElementsByTagName('title')[0];
		this.inboxText = 'Inbox';
		this.chatText = [
							{value:'\u2026', chars: 1},
							{value:'...', chars: 3}
						];
		this.timer;
		this.icons = {
			chat:'data:image/x-icon;base64,AAABAAEAEBAAAAEACABoBQAAFgAAACgAAAAQAAAAIAAAAAEACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADWRQUANjo4AC6wagA0sW0AABqQAAAgsAAAJs8AACzwABE9/wAxV/8AUXH/AHGL/wCRpf8Asb//ANHa/wDt7e0AAAAAAAAaLwAALVAAAD9wAABRkAAA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AC8ALQBQAD8AcABSAJAAYwCwAHYAzwCIAPAAmRH/AKYx/wC0Uf8AwnH/AM+R/wDcsf8A69H/AP///wAAAAAACAAvAA4AUAAVAHAAGwCQACEAsAAmAM8ALADwAD4R/wBYMf8AcVH/AIxx/wCmkf8Av7H/ANrR/wD///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJydTUVFRUVFKSkpMCAIAABciUlZUVFRTUVFTUAwJAAAXIVFTVVVRSEpKU1AMCQAAGCFRU1FIOEBISlFQDAoAABgjUVFDNzs8QUZKTQsHAAAYI1FCNTAUFDE/RVELBwAAGCVANC0PHBwOLjZEDAYAAB0pOR8PLEdLKgEkOg0HAAAaGhURM1RWVlcyDxETBQAAIB4jPVZWVlZWVj4bFhIAACgvSVZWVlZWVVVVSysmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//AAD//wAAgAEAAIABAACAAQAAgAEAAIABAACAAQAAgAEAAIABAACAAQAAgAEAAIABAAD//wAA//8AAP//AAA=',
		};
		this.pixelMaps = {
			icons: {
				'unread':
					[
						['','','','','','','','','','','','','','','',''],
						['','','','','','','','','','','','','','','',''],
						['','','','','','','','','','','','','','','',''],
						['','#306dd9','#4c7fee','#d8d8dd','#ededed','#ededed','#ededed','#ededed','#ededed','#ededed','#ebebed','#ebebed','#d6d6e0','#3e76ee','#2662d6',''],
						['','#1062ff','#0860ff','#1465fe','#adb9dd','#ededed','#ededed','#ededed','#ededed','#ededed','#ededed','#b2b9cd','#0b5afa','#0057ff','#0657ec',''],
						['','#0e5df5','#0e5df5','#0052fd','#004dff','#6693f2','#eae9e9','#ededed','#ededed','#f0ebe7','#6390f1','#0044ff','#004dff','#0a54e1','#0548cb',''],
						['','#0d5ff8','#236df8','#8da1d6','#1f62ed','#0044ff','#3271f2','#d7d8de','#d6d6e0','#3972e7','#003bff','#1e65f2','#92ace5','#1758d4','#0545d6',''],
						['','#0659f8','#1c6cff','#c7c7c6','#9a9a9a','#4470ce','#0044ff','#085eff','#085eff','#0042ff','#4477e6','#b1b0b0','#d0d0cf','#0b54df','#0543d5',''],
						['','#0659f8','#1465fe','#e1e1e1','#cbcbcb','#aeaca8','#748ac3','#0051fa','#0051fa','#7d97db','#c7c4bf','#d3d3d3','#e1e1e1','#0a52dc','#0545d6',''],
						['','#0659f8','#1465fe','#e1e1e1','#e1e1e1','#cecece','#b4b3b0','#a6adcc','#acb2d1','#c8c8c5','#d5d5d5','#dddddd','#dddfe2','#0a52dc','#0545d6',''],
						['','#0659f8','#1264ff','#e1e1e1','#e4e4e4','#e1e1e1','#d9d9d9','#bfbfbf','#c7c7c6','#d9d9d9','#dddddd','#e1e1e1','#dee0e5','#0b54df','#0545d8',''],
						['','#0358fc','#1264ff','#e1e1e1','#e4e4e4','#ebebed','#ebebed','#e1e1e1','#d9d9d9','#dddddd','#dddddd','#e4e4e4','#dee0e5','#0b54df','#0245dd',''],
						['','#0358fc','#1564f9','#e3e3e5','#ededed','#eae9e9','#eae9e9','#eae9e9','#e4e4e4','#e1e1e1','#e1e1e1','#e4e4e4','#dee0e5','#0b54df','#0245dd',''],
						['','#2d66d1','#2d66d1','#e4e4e4','#e1e1e1','#e1e1e1','#e1e1e1','#e1e1e1','#e1e1e1','#dddddd','#dddddd','#dddddd','#d9dce2','#0041d9','#36559c',''],
						['','','','','','','','','','','','','','','',''],
						['','','','','','','','','','','','','','','','']
					]
				},
			numbers: [
				[
					[0,1,1,0],
					[1,0,0,1],
					[1,0,0,1],
					[1,0,0,1],
					[0,1,1,0]
				],
				[
					[0,1,0],
					[1,1,0],
					[0,1,0],
					[0,1,0],
					[1,1,1]
				],
				[
					[1,1,1,0],
					[0,0,0,1],
					[0,1,1,0],
					[1,0,0,0],
					[1,1,1,1]
				],
				[
					[1,1,1,0],
					[0,0,0,1],
					[0,1,1,0],
					[0,0,0,1],
					[1,1,1,0]
				],
				[
					[0,0,1,0],
					[0,1,1,0],
					[1,0,1,0],
					[1,1,1,1],
					[0,0,1,0]
				],
				[
					[1,1,1,1],
					[1,0,0,0],
					[1,1,1,0],
					[0,0,0,1],
					[1,1,1,0]
				],
				[
					[0,1,1,0],
					[1,0,0,0],
					[1,1,1,0],
					[1,0,0,1],
					[0,1,1,0]
				],
				[
					[1,1,1,1],
					[0,0,0,1],
					[0,0,1,0],
					[0,1,0,0],
					[0,1,0,0]
				],
				[
					[0,1,1,0],
					[1,0,0,1],
					[0,1,1,0],
					[1,0,0,1],
					[0,1,1,0]
				],
				[
					[0,1,1,0],
					[1,0,0,1],
					[0,1,1,1],
					[0,0,0,1],
					[0,1,1,0]
				],
			]
		};
		
		this.timer = setInterval(this.poll, 500);
		this.poll();
		
		return true;
	}
	
	this.drawUnreadCount = function(unread) {
		if(!self.textedCanvas) {
			self.textedCanvas = [];
		}
		
		if(!self.textedCanvas[unread]) {
			var iconCanvas = self.getUnreadCanvas();
			var textedCanvas = document.createElement('canvas');
			textedCanvas.height = textedCanvas.width = iconCanvas.width;
			var ctx = textedCanvas.getContext('2d');
			ctx.drawImage(iconCanvas, 0, 0);
			
			ctx.fillStyle = "#fef4ac";
			ctx.strokeStyle = "#dabc5c";
			ctx.strokeWidth = 1;
			
			var count = unread.length;
			var bgHeight = self.pixelMaps.numbers[0].length;
			var bgWidth = 0;
			var padding = count > 2 ? 0 : 1;
			
			for(var index = 0; index < count; index++) {
				bgWidth += self.pixelMaps.numbers[unread[index]][0].length;
				if(index < count-1) {
					bgWidth += padding;
				}
			}
			bgWidth = bgWidth > textedCanvas.width-4 ? textedCanvas.width-4 : bgWidth;
			
			ctx.fillRect(textedCanvas.width-bgWidth-4,2,bgWidth+4,bgHeight+4);
			
			
			var digit;
			var digitsWidth = bgWidth;
			for(var index = 0; index < count; index++) {
				digit = unread[index];
				if (self.pixelMaps.numbers[digit]) {
					var map = self.pixelMaps.numbers[digit];
					var height = map.length;
					var width = map[0].length;
					
					
					ctx.fillStyle = "#2c3323";
					
					for (var y = 0; y < height; y++) {
						for (var x = 0; x < width; x++) {
							if(map[y][x]) {
								ctx.fillRect(14- digitsWidth + x, y+4, 1, 1);
							}
						}
					}
					
					digitsWidth -= width + padding;
				}
			}	
			
			ctx.strokeRect(textedCanvas.width-bgWidth-3.5,2.5,bgWidth+3,bgHeight+3);
			
			self.textedCanvas[unread] = textedCanvas;
		}
		
		return self.textedCanvas[unread];
	}
	this.getUnreadCanvas = function() {
		if(!self.unreadCanvas) {
			self.unreadCanvas = document.createElement('canvas');
			self.unreadCanvas.height = self.unreadCanvas.width = 16;
			
			var ctx = self.unreadCanvas.getContext('2d');
			
			for (var y = 0; y < self.unreadCanvas.width; y++) {
				for (var x = 0; x < self.unreadCanvas.height; x++) {
					if (self.pixelMaps.icons.unread[y][x]) {
						ctx.fillStyle = self.pixelMaps.icons.unread[y][x];
						ctx.fillRect(x, y, 1, 1);
					}
				}
			}
		}
		
		return self.unreadCanvas;
	}
	this.getChat = function() { return false || GM_getValue('chatEnabled', true); }
	this.getDebugging = function() { return false || GM_getValue('debuggingEnabled', false); }
	this.getSearchElement = function() {
		var element;
		var nav = document.body.getElementsByClassName('n0');

		if(nav.length) {
			var potential = nav[0];
			
			if(potential.className.indexOf('n0') !== -1) {
				element = potential;
			}
		}
		
		return element ? element: null;
	}
	this.newChat = function() {
		var title = self.title.innerHTML;
		for(var index in self.chatText) {
			var location = title.indexOf(self.chatText[index].value);
			if(self.chatText[index].chars + location == title.length) {
				return true;
			}
		}
		return false;
	}
	this.newMail = function() { return self.searchElement.textContent.match(/\((\d*)\)/); }
	this.getUnreadCountDisplay = function() { return GM_getValue('unreadCountDisplay', true); }
	this.getUnreadCount = function() {
		if(this.newMail()) {
			matches = self.searchElement.textContent.match(/\((\d*)\)/);
			return matches ? matches[1] : false;
		}
	}
	this.getUnreadCountIcon = function() {
		var unread = self.getUnreadCount();		
		if(this.getUnreadCountDisplay()) {
			return self.drawUnreadCount(unread).toDataURL('image/png');
		} else {
			return self.icons.unread;
		}
	}
	
	this.poll = function() {
		self.searchElement = self.getSearchElement();
    
    if(!self.searchElement) {
      // We didn't find the searchElement, try again
      // on the next poll.
      return;
    }
		
		if(self.getChat() && self.newChat()) {
			return self.setIcon(self.icons.chat);
		}
			
		if(self.newMail())
			self.setIcon(self.getUnreadCountIcon());
		else
			self.setIcon(self.icons.read);
	}
	
	this.setIcon = function(icon) {
		var links = self.head.getElementsByTagName("link");
		for (var i = 0; i < links.length; i++)
			if ((links[i].rel == "shortcut icon" || links[i].rel=="icon") &&
			   links[i].href != icon)
				self.head.removeChild(links[i]);
			else if(links[i].href == icon)
				return;

		var newIcon = document.createElement("link");
		newIcon.type = "image/png";
		newIcon.rel = "shortcut icon";
		newIcon.href = icon;
		
		self.head.appendChild(newIcon);
		
    setTimeout(function() {
      var shim = document.createElement('iframe');
      shim.width = shim.height = 0;
      document.body.appendChild(shim);
      shim.src = "icon";
      document.body.removeChild(shim);

      console.log("After setting the icon.");
    }, 499);
  }
	
	this.toString = function() { return '[object GmailFavIconAlerts]'; }
	
	return this.construct();
}