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

    @@ -4,73 +4,69 @@ * @include https://www.google.* * @license MIT License */ - (function(document){ - function queryIds(nodes, text) { - var n = nodes.pop(); - var url = n.href.replace(/,/g, '%252C'); - if (nodes.length == 0) { - var t = text + url; - var q = t.replace(/#/g, '%23').replace(/&/g, '%26').replace(/=/g, '%3D').replace(/\?/g, '%3F'); - return q; - } + (function(global){ + var document = global.document; - var t = text + url + ','; - return queryIds(nodes, t); - } + var nodes = Array.from(document.querySelectorAll('h3.r > a[onmousedown]')); + var ids = nodes.reduce((ids, node, i) => { + var t = ids + node.href.replace(/,/g, '%252C');; + if (i == nodes.length - 1) { + return t.replace(/#/g, '%23').replace(/&/g, '%26').replace(/=/g, '%3D').replace(/\?/g, '%3F'); + } - function loadJSONP(url) { + return t + ','; + }, ''); + var script = document.createElement('SCRIPT'); - script.src = url; + script.src = 'https://graph.facebook.com/?ids='+ids+'&callback=callback'; document.body.insertBefore(script, null); - } - function 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); - if (info) { - counter.href = info.id; - counter.innerText = info.shares || 0; - } else { - counter.style.setProperty('background-color', '#FF1000'); - counter.innerText = 'X'; - } - - return counter; - } + global.callback = function(data){ + if (data.error) { + alert('[FB]'+data.error.message); + return; + } - function infoFrom(response, key){ - var id = Object.keys(response).filter( (k) => { - return key.indexOf(k) > -1; - })[0]; - return response[id]; - } + nodes.map((a) => { + let u; + try { + u = decodeURIComponent(a.href); + } catch (e){ + u = a.href; + } + var url = u.replace(/,/g, '\u00252C'); + var info = infoFrom(data, url); + var c = counter(info); + var parentNode = a.parentNode; + parentNode.style.setProperty('overflow', 'visible'); + parentNode.appendChild(c); + }); - window.callback = function(data){ - if (data.error) { - alert('[FB]'+data.error.message); - return; - } + function 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); + if (info) { + counter.href = info.id; + counter.innerText = info.shares; + if (info.share){ + counter.innerText = info.share.share_count; + } + } else { + counter.style.setProperty('background-color', '#FF1000'); + counter.innerText = 'X'; + } - nodes.map((a) => { - let u; - try { - u = decodeURIComponent(a.href); - } catch (e){ - u = a.href; + return counter; } - var url = u.replace(/,/g, '\u00252C'); - var info = infoFrom(data, url); - var c = counter(info); - var parentNode = a.parentNode; - parentNode.style.setProperty('overflow', 'visible'); - parentNode.appendChild(c); - }); - }; - var nodes = Array.from(document.querySelectorAll('h3.r > a[onmousedown]')); - var ids = queryIds(nodes.slice(), ""); - loadJSONP('https://graph.facebook.com/?ids='+ids+'&callback=callback'); + function infoFrom(response, key){ + var id = Object.keys(response).filter( (k) => { + return key.indexOf(k) > -1; + })[0]; + return response[id]; + } + }; - })(document); + })(window);
  • /*
     * @title Google検索の結果にFacebookのシェア数を表示するブックマークレット
     * @description 同上
     * @include https://www.google.*
     * @license MIT License
     */
    
    (function(global){
      var document = global.document;
    
      var nodes = Array.from(document.querySelectorAll('h3.r > a[onmousedown]'));
      var ids = nodes.reduce((ids, node, i) => {
        var t = ids + node.href.replace(/,/g, '%252C');;
        if (i == nodes.length - 1) {
          return t.replace(/#/g, '%23').replace(/&/g, '%26').replace(/=/g, '%3D').replace(/\?/g, '%3F');
        }
    
        return t + ',';
      }, '');
    
      var script = document.createElement('SCRIPT');
      script.src = 'https://graph.facebook.com/?ids='+ids+'&callback=callback';
      document.body.insertBefore(script, null);
    
      global.callback = function(data){
        if (data.error) {
          alert('[FB]'+data.error.message);
          return;
        }
    
        nodes.map((a) => {
          let u; 
          try {
            u = decodeURIComponent(a.href); 
          } catch (e){
            u = a.href;
          }
          var url = u.replace(/,/g, '\u00252C');
          var info = infoFrom(data, url); 
          var c = counter(info);
          var parentNode = a.parentNode;
          parentNode.style.setProperty('overflow', 'visible');
          parentNode.appendChild(c);
        });
    
        function 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);
          if (info) {
            counter.href = info.id;
            counter.innerText = info.shares;
            if (info.share){
              counter.innerText = info.share.share_count;
            }
          } else {
            counter.style.setProperty('background-color', '#FF1000');
            counter.innerText = 'X';
          }
    
          return counter;
        }
    
        function infoFrom(response, key){
          var id = Object.keys(response).filter( (k) => {
            return key.indexOf(k) > -1;
          })[0];
          return response[id];
        }
      };
    
    })(window);
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

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