basyura's blog

あしたになったらほんきだす。

ブクマコメントをポップアップで表示するグリモン - プロトタイプ

m キーを押すと、はてぶAPIから json データを取得してポップアップで表示。再度 m を押すとコメントを消す。

livedoor reader を使ってて「みんなどんなコメント書いてるんだろう?」と思ってから確認するまでの手間が不便だったので書いてみた。
書き始めてから 1 時間半ぐらいかな。とりあえず表示できましたというレベル。今後ブラッシュアップ予定。

// ==UserScript==
// @version     0.0.1
// @name        LDR Show Comments
// @namespace   http://basyura.org
// @include     http://reader.livedoor.com/reader/*
// @author      basyura
// ==/UserScript==

(function (w) {
	
var jsonapiurl  = "http://b.hatena.ne.jp/entry/json/";
		
function showComments(link) {
	var opt = {
		method: 'GET',
		url: jsonapiurl + link,
		onload: function(res){
			var bookmarks = eval(res.responseText).bookmarks;
			var length    = bookmarks.length;
			var buf = [];
			buf.push("<table>");
			for(var i = 0 ; i < length ; i++) {
				var b = bookmarks[i];
				if(b.comment == "") {
					continue;
				}
				buf.push("<tr><td valign='top'><img src='http://www.hatena.ne.jp/users/ba/");
				buf.push(b.user);
				buf.push("/profile_s.gif'></td><td valign='top'>");
				buf.push(b.user);
				buf.push("</td valign='top'><td>");
				buf.push(b.comment);
				buf.push("</td></tr>");
			}
			var comment = document.getElementById("hatena_bookmark_comment");
			if(comment == null) {
				comment = document.createElement("div");
				comment.setAttribute("id"    , "hatena_bookmark_comment");
				comment.setAttribute("style" , "position:absolute;");
				comment.setAttribute("align" , "middle");
				document.body.appendChild(comment);
			}
			comment.innerHTML = "<div style='width:70%;height:400px;overflow-y:auto;background-color:#f0f0f0;border:2px solid gray;' align='left'>" + buf.join("") + "</div>";
			comment.focus();
		},
		onerror: function(res){
		},
	}
	window.setTimeout(GM_xmlhttpRequest, 0, opt);
}

w.register_hook('after_init', function() {
	w.Keybind.add('m', function() {
		var comment = document.getElementById("hatena_bookmark_comment");
		if(comment != undefined) {
			document.body.removeChild(comment);
		}
		else {
			showComments(w.get_active_item(true).link);
		}
	});
});
})(this.unsafeWindow || this);