/*
* @title Bookmark Count @ jinriki
* @description Add bookmark count to Hatena Question
* @include http://q.hatena.ne.jp/*
* @license MIT License
* @javascript_url
*/
var a_kuma3_bookmark = {
add_bookmark_count: function (result) {
var copy_attr = function(dest, from) {
for (var k in from) { dest[k] = from[k]; }
};
Array.prototype.forEach.call(document.body.querySelectorAll('A.question-content-container'), function(link) {
var bm_count = result[link.href];
if (typeof(bm_count) == "number") { // bookmark count can be zero
if (bm_count > 0) {
var bm_ele = document.createElement("A");
copy_attr(bm_ele.style, {
color: (bm_count > 1 ? "#ff2c6f" : "gray"),
fontWeight: (bm_count > 1 ? "bolder" : "normal"),
textDecoration: "none",
fontSize: "90%",
marginLeft: "1em",
});
copy_attr(bm_ele, {
innerHTML: bm_count + " user" + (bm_count > 1 ? "s" : ""),
href: "http://b.hatena.ne.jp/entry/" + link.href,
target: "_blank",
title: "ブックマーク数",
});
link.nextSibling.appendChild(bm_ele);
}
link.bookmark_count_added = true;
}
});
if (this.remained_to_process) {
this.get_bookmark_count(document.body);
}
},
get_bookmark_count: function (context) {
// hatena bookmark getcount API
// http://developer.hatena.ne.jp/ja/documents/bookmark/apis/getcount
this.remained_to_process = false;
var request_link_count = 0;
var self = this;
var request_url = "http://api.b.st-hatena.com/entry.counts?callback=a_kuma3_bookmark.add_bookmark_count&" +
Array.prototype.filter.call(context.querySelectorAll('A.question-content-container'),
function(link, index, array) {
if (! link.bookmark_count_added) {
if (request_link_count < 50) {
request_link_count += 1;
return true;
} else {
self.remained_to_process = true;
}
}
return false;
})
.map(function(link) {
return "url=" + link.href;
})
.join("&");
if (request_link_count > 0) {
var script = document.createElement("script");
script.src = request_url;
document.body.appendChild(script);
console.log("!!!! (" + request_link_count + ") --- " + request_url);
}
},
run: function() {
this.get_bookmark_count(document.body);
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
var MutationObserver = window.MutationObserver || window.WebkitMutationObserver;
new MutationObserver(function (records) {
records.forEach(function (record) {
if (record.addedNodes) {
if (Array.prototype.find.call(record.addedNodes, function(e) {
return e.tagName == "UL" && /list-question/.exec(e.className);
})) {
a_kuma3_bookmark.get_bookmark_count(document.body);
}
}
});
}).observe(document.body, { childList: true, subtree: true });
},
};
a_kuma3_bookmark.run();
// another method, maybe easier...
// http://b.hatena.ne.jp/entry/image/