Google検索の結果にFacebookのシェア数を表示するブックマークレット

    @@ -8,11 +8,12 @@ (function(global){ var document = global.document; - var nodes = Array.from(document.querySelectorAll('.r > a[ping]')); + var nodes = Array.from(document.querySelectorAll('.r > a:first-child')); var ids = nodes.map(n=>n.href).join(','); var script = document.createElement('SCRIPT'); script.src = 'https://graph.facebook.com/?fields=og_object{engagement}&ids='+ids+'&callback=callback'; + document.body.insertBefore(script, null); global.callback = function(data){
  • /*
     * @title Google検索の結果にFacebookのシェア数を表示するブックマークレット
     * @description 同上
     * @include https://www.google.*
     * @license MIT License
     */
    
    (function(global){
      var document = global.document;
    
      var nodes = Array.from(document.querySelectorAll('.r > a:first-child'));
      var ids = nodes.map(n=>n.href).join(',');
    
      var script = document.createElement('SCRIPT');
      script.src = 'https://graph.facebook.com/?fields=og_object{engagement}&ids='+ids+'&callback=callback';
    
      document.body.insertBefore(script, null);
    
      global.callback = function(data){
        if (data.error) {
          alert('[FB]'+data.error.message);
          return;
        }
    
        var counter = (info) => {
            var STYLE = "padding: 0 0.5em;\nmargin: 0 0.5em;\ncolor: #fff;\ntext-shadow: 0 1px 0 white;\nbackground-color: #4e69a2;\nfont-weight: bold;\ntext-decoration-line: none;";
            var counter = document.createElement('A');
            counter.setAttribute('style', STYLE);
            
            counter.innerText = info.engagement.count || 'X';
            if (info) {
              counter.href = info.id;
            } else {
              counter.style.setProperty('background-color', '#FF1000');
            }
      
            return counter;
        };
      
        for (var i in nodes) {
          var a = nodes[i];
          var url = a.href;
          var og_object = data[url] ? data[url].og_object : null;
          if (!og_object) continue;
          
          var c = counter(og_object);
          var parentNode = a.parentNode;
          parentNode.style.setProperty('overflow', 'visible');
          parentNode.appendChild(c);
        }
      };
    })(window);
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2020/03/22 17:55:18 - 2020-03-22
  2. 2019/10/03 22:42:03 - 2019-10-03
  3. 2019/10/03 22:37:52 - 2019-10-03
  4. 2018/09/28 19:23:39 - 2018-09-28
  5. 2018/01/31 12:46:35 - 2018-01-31
  6. 2016/10/16 18:41:10 - 2016-10-16
  7. 2016/08/27 01:03:12 - 2016-08-27
  8. 2016/08/26 17:01:07 - 2016-08-26
  9. 2016/08/26 16:47:13 - 2016-08-26
  10. 2016/02/26 22:24:42 - 2016-02-26
  11. 2015/11/30 14:00:08 - 2015-11-30
  12. 2015/11/30 13:49:05 - 2015-11-30
  13. 2015/11/30 13:48:04 - 2015-11-30
  14. 2015/11/28 01:44:13 - 2015-11-28
  15. 2015/11/27 23:20:41 - 2015-11-27
  16. 2015/11/27 23:20:09 - 2015-11-27
  17. 2015/11/27 23:08:00 - 2015-11-27
  18. 2015/11/27 23:05:44 - 2015-11-27
  19. 2015/11/27 23:03:47 - 2015-11-27
  20. 2015/11/27 22:56:26 - 2015-11-27
  21. 2015/11/27 22:53:39 - 2015-11-27
  22. 2015/11/27 22:53:05 - 2015-11-27
  23. 2015/11/27 22:51:16 - 2015-11-27
  24. 2015/11/27 22:49:19 - 2015-11-27
  25. 2015/11/27 22:47:53 - 2015-11-27
  26. 2015/11/27 22:45:18 - 2015-11-27
  27. 2015/11/27 22:45:04 - 2015-11-27
  28. 2015/11/27 22:42:17 - 2015-11-27
  29. 2015/11/27 22:41:26 - 2015-11-27
  30. 2015/11/27 22:34:42 - 2015-11-27
  31. 2015/11/27 22:32:08 - 2015-11-27
  32. 2015/11/27 22:26:12 - 2015-11-27
  33. 2015/11/27 22:13:07 - 2015-11-27
  34. 2015/11/27 22:12:21 - 2015-11-27
  35. 2015/11/27 22:11:44 - 2015-11-27
  36. 2015/11/27 22:07:37 - 2015-11-27
  37. 2015/11/27 21:11:33 - 2015-11-27
  38. 2015/11/27 20:41:58 - 2015-11-27
  39. 2015/11/27 13:34:13 - 2015-11-27
  40. 2015/11/27 13:31:45 - 2015-11-27
  41. 2015/11/27 13:18:44 - 2015-11-27
  42. 2015/11/27 01:39:48 - 2015-11-27