SNSのシェアカウントをJavaScriptで取得する
各SNSのシェアカウントをJavaScript(jQuery使用)で取得したくてググったら便利なサードパーティーAPIを見つけたのでそれを使ってシェアカウントを取得してみました。(ついでにはてなブックマークも)
サードパーティーAPIなのでいつサービスが終わるか分からない恐怖に怯えながら使用してください(・.・;)
/* プラグイン部分 */ (function ($, window) { var getSharedCount = function(fn, arg) { if ('withCredentials' in new XMLHttpRequest) { arg.success = fn; } else { var cb = 'sc_' + (0 + new Date()); window[cb] = fn; arg.jsonpCallback = cb; if (arg.dataType == 'json') arg.dataType += 'p'; } return $.ajax(arg); } $.sharedCount = function(url, fn) { url = encodeURIComponent(url || location.href); var arg = { url: '//' + (location.protocol == 'https:' ? 'sharedcount.appspot' : 'api.sharedcount') + '.com/?url=' + url, cache: true, dataType: 'json' }; getSharedCount(fn, arg); }; $.hatebuCount = function(url, fn) { url = url || location.href; var arg = { url: 'http://api.b.st-hatena.com/entry.count?callback=?', dataType: 'jsonp', data: { url: url } }; getSharedCount(fn, arg); }; })(jQuery, window); /* 実行部分 */ (function($, window) { $(function(){ var url = 'http://jsdo.it/'; $.sharedCount(url, function(data) { $('#count-twitter').text(data.Twitter); $('#count-facebook').text(data.Facebook.like_count); $('#count-googleplus').text(data.GooglePlusOne); $('#count-linkedin').text(data.LinkedIn); $('#count-delicious').text(data.Delicious); $('#count-pinterest').text(data.Pinterest); }); $.hatebuCount(url, function(data) { $('#count-hatebu').text(data); }); }); })(jQuery, window);