Skip to content

Instantly share code, notes, and snippets.

@uu59
Created May 26, 2011 06:12
Show Gist options
  • Save uu59/992638 to your computer and use it in GitHub Desktop.
Save uu59/992638 to your computer and use it in GitHub Desktop.
// http://i.imgur.com/Gtr6C.png
(function(){
liberator.echo("hello hbc");
const RE_URL = new RegExp('https?://[^/]+/?.*?(?:¥n| |$|¥Z)', 'ig');
function escape_breaker(str){
// liberator.echo print plain text if invalid formed HTML given
var re = new RegExp('[><&+]', 'g');
return str.replace(re, function(c,p,full) {
return "&#"+(c.charCodeAt(0))+";";
});
}
function start(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", 'http://b.hatena.ne.jp/entry/jsonlite/?url='+encodeURIComponent(url), true);
xhr.onreadystatechange = function() {
if(this.readyState == 4) {
var response = JSON.parse(xhr.responseText);
if(!response) {
liberator.echoerr("Does not exists!!");
return ;
}
var bookmarks = response["bookmarks"];
var tags = {};
var html = '<div><strong highlight="CompTitle" style="display:block;">'+(response.count)+' users / '+response.title+'</strong>'+
'<ul style="margin:0;padding:0;">';
bookmarks.forEach(function(bookmark){
if(!bookmark.comment || bookmark.comment.length == 0) {
return false;
}
var d = new Date(bookmark.timestamp);
var ymd = [d.getFullYear(), d.getMonth()+1, d.getDate()].map(function(n){
return n > 9 ? n : "0"+n;
}).join('-');
bookmark.tags.forEach(function(tag){
if(!tags[tag]) {
tags[tag]=0;
}
tags[tag] += 1;
});
html += '<li style="list-style:none;font-size:small;margin:0.3ex 0;padding:0;display:table;width:100%;">'+
'<a href="http://b.hatena.ne.jp/'+bookmark.user+'" style="display:table-cell;width:20ex;text-align:right;padding-right:1ex;" highlight="URL">'+bookmark.user+'</a> ' +
'<span style="display:table-cell;white-space:normal;" highlight="CompItem">' + ymd + ' ' + escape_breaker(bookmark.comment).replace(RE_URL, "<a highlight='URL' href='$&' style='display:inline;'>$&</a>") +'</span>' +
'<span style="display:table-cell;width:10ex;text-align:right;padding-left:1ex;" highlight="CompDesc">' +
(bookmark.tags && bookmark.tags.length > 0 ?
escape_breaker(bookmark.tags.join(' ')) : ''
) +
'</span>' +
"</li>"
;
});
html += '</ul><div highlight="CompDesc" style="font-size:small;white-space:pre-wrap;">'+
'<strong highlight="CompTitle" style="display:block;">Tags</strong>'
;
var buf = [];
for(var tag in tags) {
buf.push({"count": tags[tag], "tag": tag});
}
buf.sort(function(b,a){
return a.count - b.count;
});
buf.forEach(function(obj){
var estag = escape_breaker(obj.tag);
html += '<a highlight="URL" href="http://b.hatena.ne.jp/t/'+estag+'" style="display:inline;">'+estag+'('+obj.count+') </a>';
});
html = html +'</div></div>';
liberator.echo(html, true);
}
}
xhr.send(null);
}
liberator.modules.commands.addUserCommand(["hatenabookmarkcomments", "hbc"],"show Hatena Bookmark comments",
function(args){
start(liberator.modules.buffer.URL);
},{
},true
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment