// ==UserScript==
// @name Favicon with Hatena Bookmark
// @namespace	http://libelabo.jp/
// @description	A script to add favicons next to posted links on Hatena Bookmark 
// @include	http://b.hatena.ne.jp/*
// ==/UserScript==


(function(){

// apply the function to each element matched by the path
function forEachMatch(path, f, root) {
	var el;
	var root = (root == null) ? document : root;
	var matches = root.evaluate(
		path, root, null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	for (var i = 0; el=matches.snapshotItem(i); i++)
		f(el);
}

// adds the link favicon before itself
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='bookmark'] | //a[@class='news_title']",
	add_favicon);
}())

