// ==UserScript==
// @name Favicon with Google
// @namespace	http://libelabo.jp/
// @description	A script to add favicons next to links on Google search results 
// @include	http://*google.*/*q=*
// @exclude	http://mail.google.com/*
// ==/UserScript==

(function(){

// apply the function to each element found by the path
function forEachMatch(path, f, root) {
	var root = (root == null) ? document : root;
	var matches = root.evaluate(
		path, root, null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	for (var i = 0; i < matches.snapshotLength; i++)
		f(matches.snapshotItem(i));
}

// adds the link favicon before itselft
function add_favicon(link) {
	var favicon = document.createElement('img');
	favicon.src = "http://" + link.hostname + "/favicon.ico";
	favicon.width = 16;
	favicon.alt   = "";
	favicon.style.marginRight = "1ex";
	link.parentNode.insertBefore(favicon, link);
}

// apply to all recent links, popular and your bookmarks
forEachMatch(
	"//a[@class='l']",add_favicon);
}())
