// ==UserScript==
// @name           HatebuPercentage
// @namespace      http://www.kmonos.net/
// @author         k.inaba
// @include        http://b.hatena.ne.jp/*
// @description    Modify the link text to a 'Hatena-Bookmark' entry page and show how many of the users have left comments on the entry
// ==/UserScript==

// Released under the NYSL license
//  http://www.kmonos.net/nysl/

function addInfo(a) {
	var url = a.getAttribute("href")
	GM_xmlhttpRequest({
		method: "GET",
		   url: url,
		onload: function(details) {
			var text = details.responseText
			var cur  = 0

			var total       = 0
			var withComment = 0

			for(;;) {
				cur = text.indexOf( '<li id="bookmark-user-', cur )
				if( cur == -1 ) break

				cur = text.indexOf('</li>', cur)
				for(var e=cur-1; text.charAt(e)!='>'; --e)
					if( text.charAt(e)!=' ' )
						{++withComment; break}

				++ total
			}

			var pc = Math.floor(total==0 ? 0 : withComment / total * 100);
			a.innerHTML = a.innerHTML + " / " + pc +"%"
	}})
}

var re1 = /^(http:\/\/b\.hatena\.ne\.jp)?\/entry\/http/
var re2 = /users$/

function isHBMLink(a) {
	return re1.test(a.href) && re2.test(a.innerHTML)
}

var as = document.getElementsByTagName("a")
for(var i=0; i<as.length; ++i)
	if( isHBMLink(as[i]) )
		addInfo(as[i])
