Gmail Label Colors NG

By KARASZI István Last update Nov 19, 2007 — Installed 1,722 times. Daily Installs: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
// Copyright 2007 Istvan Karaszi <gmaillabelcolors-ng@spam.raszi.hu>.
// All rights Reserved.

// ==UserScript==
// @name          Gmail Label Colors NG
// @namespace     http://persistent.info/greasemonkey
// @description	  v0.3 Optionally colors the labels if they have a #color suffix (eg. "flickr ##FF0084 #white"). It's compatible with the new GMail design (GMail2.0)
// @include       http://mail.google.com/*
// @include       https://mail.google.com/*
// ==/UserScript==

const THREADLIST = "tm";
const CSSRULES = [
	"span.colored { padding: 2px 4px 2px 4px; -moz-border-radius: 2px; }"
];
const OLDNODES = [
	"//div[contains(@class, 'lk')]", 
	"//span[contains(@class, 'lk')]", 
	"//b[contains(@class, 'lk')]", 
	"//span[contains(@class, 'ct')]"
];
const NEWNODES = [
	".//table//tr/td//div[@idlink='']/span",
	".//table//tr/td[5]/span[1]",
	".//h1[@class]/span[2]/span"
];

var LabelRules = new Array();

window.addEventListener('load', function() {
	// apply css rules
	_ApplyCSSRules();

	if (!unsafeWindow.gmonkey) {
		_AddGlobalStyles();
		return;
	}

	unsafeWindow.gmonkey.load('1.0', function( gmail ) {
		function _GMailLabelColors() {
			var search = [
				gmail.getNavPaneElement(),
				gmail.getActiveViewElement()
			];
			for(var h = 0; h < search.length; h++) {
				var currentContext = search[h];

				for(var i = 0; i < NEWNODES.length; i++) {
					var results = _XPath(NEWNODES[i], currentContext);
					for(var j = 0; j < results.snapshotLength; j++) {
						var item = results.snapshotItem(j)

						_ApplyColorize(item);
					}
				}
			}
		}

		gmail.registerViewChangeCallback(_GMailLabelColors);
		_GMailLabelColors();
	});
}, true);


function _ApplyCSSRules() {
	for(var i = 0; i < OLDNODES.length; i++) {
		var results = _XPath(OLDNODES[i]);
		for (var j = 0; j < results.snapshotLength; j++) {
			_ApplyColorize(results.snapshotItem(j));
		}
	}
}

function _ApplyColorize( item ) {
	var c;
	if ((c = _GetColors(item.innerHTML))) {
		//item.innerHTML = String.concat('<span class="colored" style="', c[1], '">', c[0], '</span>');

		// add classname
		item.className += ' ' + "colored";

		// set colors
		var st = item.style;
		st.color = c[1][0];
		st.backgroundColor = c[1][1];

		// remove the unwanted color tags
		item.innerHTML = c[0];
	}
}

function _GetColors( str ) {
	var s = new String(str);
	var m;

	if ((m = s.match(/^(.*?)\s+#(#?\w+)\s*(#(#?\w+))?/))) {
		var str = m[0];
		var label = m[1];

		if (!LabelRules[str]) {
			if (m.length == 5) {
				LabelRules[str] = _GetCSS(m[2], m[4]);
			} else {
				LabelRules[str] = _GetCSS(m[2], null);
			}
		}

		return new Array(label, LabelRules[str]);
	}

	return null;
}

function _XPath( query, context ) {
	if (!context) context = document;
	return document.evaluate(query, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}


function _AddGlobalStyles() {
	var head, style, ihtml;

	if ((head = document.getElementsByTagName("head")[0])) {
		ihtml = "";
		for (var i = 0; i < CSSRULES.length; i++) {
			ihtml += CSSRULES[i];
		}

		style = document.createElement("style");
		style.type = "text/css";
		style.innerHTML = ihtml;

		head.appendChild(style);
	}
}

function _GetCSS( bg, fg ) {
	var c, fgColor, bgColor

	bgColor = bg;
	fgColor = (fg == null) ? "black" : fg;

	//
	// if background color is a valid hexadecimal number than we can
	// calculate the foreground
	//
	c = parseInt(bg, 16);
	if (!isNaN(c)) {
		bgColor = String.concat("#", bgColor);

		// guess a foreground color
		if (fg == null) {
			var n = new Number((c ^ parseInt("ffffff", 16)));
			fgColor = String.concat("#", n.toString(16));
		}
	}

	//
	// create css rules
	//
	return new Array(fgColor, bgColor);
}