Skip to content

Instantly share code, notes, and snippets.

@ts-3156
Created August 9, 2013 03:14
Show Gist options
  • Save ts-3156/6190911 to your computer and use it in GitHub Desktop.
Save ts-3156/6190911 to your computer and use it in GitHub Desktop.
facebook、twitter、はてなブックマークで共有された数を集計するJavaScript
function insertScript(url) {
var target = document.createElement('script');
target.charset = 'utf-8';
target.src = url;
document.body.appendChild(target);
}
function likeCount(_url) {
var pageURL = (_url) ? _url : location.href;
pageURL = encodeURIComponent(pageURL);
var callback_name = 'jsonp_fb_like';
var url = 'http://graph.facebook.com/' + pageURL + '?callback=' + callback_name;
insertScript(url);
window[callback_name] = function(data){
var count = (data.shares) ? data.shares : 0;
console.log('fb like: ' + count);
};
}
function tweetCount(_url) {
var pageURL = (_url) ? _url : location.href;
pageURL = encodeURIComponent(pageURL);
var callback_name = 'jsonp_tw';
var url = 'http://urls.api.twitter.com/1/urls/count.json?url=' + pageURL + '&callback=' + callback_name + '&noncache=' + new Date();
insertScript(url);
window[callback_name] = function(data){
console.log('tw: ' + data.count);
};
}
function hatebuCount(_url) {
var pageURL = (_url) ? _url : location.href;
pageURL = encodeURIComponent(pageURL);
var callback_name = 'jsonp_hatebu';
var url = 'http://api.b.st-hatena.com/entry.count?url=' + pageURL + '&callback=' + callback_name;
insertScript(url);
window[callback_name] = function(data){
var count = (data) ? data : 0;
console.log('hatebu: ' + count);
};
}
likeCount();
tweetCount();
hatebuCount();
// result sample
// hatebu: 1
// fb like: 56
// tw: 982
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment