ソーシャルカウントの取得方法まとめ(全9サイト)を参考にと思ったんだけど、Ajaxとかが古い感じだったので書き直し。ついでにjQuery版にしてみた。とりあえず はてブと Facebook。Twitterは別サービスの審査待ち。
$.extend($.fn, {
fb_share: function(url) {
var me = this;
$.ajax({
url: '//graph.facebook.com/?id=' + encodeURIComponent(url),
dataType: 'jsonp'
})
.then((obj) => {
var count = obj.shares || 0;
$(me).html(count);
});
},
hb_share: function(url) {
var me = this;
$.ajax({
url: 'http://api.b.st-hatena.com/entry.count?url=' + encodeURIComponent(url),
dataType: 'jsonp'
})
.then((obj) => {
var count = obj || 0;
console.log(obj);
$(me).html(count);
});
}
});
後は、
var url = document.location.href;
$(".cb-fb").fb_share(url);
$(".cb-hb").hb_share(url);
みたいな感じで取れる。