LoginSignup
3
3

More than 5 years have passed since last update.

ソーシャルサービスのブックマーク数をJavaScriptで取得する方法

Posted at

ソーシャルカウントの取得方法まとめ(全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);

みたいな感じで取れる。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3