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

    @@ -1,22 +1,39 @@ /* * @title Google検索の結果にFacebookのシェア数を表示するブックマークレット * @description 同上 - * @include https://www.google.* - * @license MIT License + * @include https://www.google.tld/search* + * @contributor laiso http://let.hatelabo.jp/laiso/let/hLHUqMrSn_0y (Fork of) + * @license MIT License http://opensource.org/licenses/MIT + * @javascript_url */ -(function(document){ -"use strict" + + +/* jshint esversion:6 */ // lame + +// - working w/ javascript:-context for https; avoid unsafe loader +// - unified quote to single +// - Arrow-func format unified: () => {} + +// TODO +// - unify variables; only "var" or "let"|"const" +// TBD +// - apply paging: pjax or Autopaging +((document) => { +'use strict'; function queryIds(nodes, text) { - var n = nodes.pop(); - var url = n.href.replace(/,/g, '%252C'); - if (nodes.length == 0) { - var t = text + url; + // FIXME assert nodes or avoid ref-exp + var url = nodes.pop().href.replace(/,/g, '%252C'); + + var t = text + url; + // TODO w/ assert + // move to function top; perhups gurad-block + if (nodes.length === 0) { var q = t.replace(/#/g, '%23').replace(/&/g, '%26').replace(/=/g, '%3D').replace(/\?/g, '%3F'); return q; } - var t = text + url + ','; + t += ','; return queryIds(nodes, t); } @@ -26,44 +43,54 @@ 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;"; +function createCounter(info) { + const STYLE = [ + 'padding: 0 0.5em;', + 'margin: 0 0.5em;', + 'color: #fff;', + 'ntext-shadow: 0 1px 0 white;', + 'background-color: #4e69a2;', + 'font-weight: bold;', + 'text-decoration-line: none;' + ].join('\n'); + var counter = document.createElement('A'); counter.setAttribute('style', STYLE); if (info) { counter.href = info.id; - counter.innerText = info.shares || 0; + counter.textContent = info.shares || 0; } else { counter.style.setProperty('background-color', '#FF1000'); - counter.innerText = 'X'; + counter.textContent = 'X'; } - + return counter; } -function infoFrom(response, key){ - var id = Object.keys(response).filter( (k) => { +function infoFrom(response, key) { + // TODO refactor + var id = Object.keys(response).filter((k) => { return key.indexOf(k) > -1; })[0]; return response[id]; } -window.callback = function(data){ +window.callback = (data) => { if (data.error) { - alert('[FB]'+data.error.message); + alert('[FB]' + data.error.message); return; } - nodes.map((a) => { - let u; - try { - u = decodeURIComponent(a.href); - } catch (e){ + nodes.map((a) => { // little strangeness + let u; + try { // TBD replace to assert + 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 info = infoFrom(data, url); + var c = createCounter(info); var parentNode = a.parentNode; parentNode.style.setProperty('overflow', 'visible'); parentNode.appendChild(c); @@ -71,7 +98,8 @@ }; var nodes = Array.from(document.querySelectorAll('h3.r > a[onmousedown]')); -var ids = queryIds(nodes.slice(), ""); -loadJSONP('https://graph.facebook.com/?ids='+ids+'&callback=callback'); +var ids = queryIds(nodes.slice(), ''); +loadJSONP('https://graph.facebook.com/?ids=' + ids + '&callback=callback'); })(document); +
  • /*
     * @title Google検索の結果にFacebookのシェア数を表示するブックマークレット
     * @description 同上
     * @include https://www.google.tld/search*
     * @contributor laiso http://let.hatelabo.jp/laiso/let/hLHUqMrSn_0y (Fork of)
     * @license MIT License http://opensource.org/licenses/MIT
     * @javascript_url
     */
    
    
    /* jshint esversion:6 */ // lame
    
    // - working w/ javascript:-context for https; avoid unsafe loader
    // - unified quote to single
    // - Arrow-func format unified: () => {}
    
    // TODO
    // - unify variables; only "var" or "let"|"const"
    // TBD
    // - apply paging: pjax or Autopaging
    ((document) => {
    'use strict';
    
    function queryIds(nodes, text) {
      // FIXME assert nodes or avoid ref-exp
      var url = nodes.pop().href.replace(/,/g, '%252C');
    
      var t = text + url;
      // TODO w/ assert
      // move to function top; perhups gurad-block
      if (nodes.length === 0) {
        var q = t.replace(/#/g, '%23').replace(/&/g, '%26').replace(/=/g, '%3D').replace(/\?/g, '%3F');
        return q;
      }
    
      t += ',';
      return queryIds(nodes, t);
    }
    
    function loadJSONP(url) {
      var script = document.createElement('SCRIPT');
      script.src = url;
      document.body.insertBefore(script, null);
    }
    
    function createCounter(info) {
      const STYLE = [
        'padding: 0 0.5em;',
        'margin: 0 0.5em;',
        'color: #fff;',
        'ntext-shadow: 0 1px 0 white;',
        'background-color: #4e69a2;',
        'font-weight: bold;',
        'text-decoration-line: none;'
      ].join('\n');
    
      var counter = document.createElement('A');
      counter.setAttribute('style', STYLE);
      if (info) {
        counter.href = info.id;
        counter.textContent = info.shares || 0;
      } else {
        counter.style.setProperty('background-color', '#FF1000');
        counter.textContent = 'X';
      }
    
      return counter;
    }
    
    function infoFrom(response, key) {
      // TODO refactor
      var id = Object.keys(response).filter((k) => {
        return key.indexOf(k) > -1;
      })[0];
      return response[id];
    }
    
    window.callback = (data) => {
      if (data.error) {
        alert('[FB]' + data.error.message);
        return;
      }
    
      nodes.map((a) => { // little strangeness
        let u;
        try { // TBD replace to assert
          u = decodeURIComponent(a.href);
        } catch (e) {
          u = a.href;
        }
        var url = u.replace(/,/g, '\u00252C');
        var info = infoFrom(data, url);
        var c = createCounter(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');
    
    })(document);
    
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2015/12/01 08:55:23 - 2015-12-01
  2. 2015/12/01 08:55:05 - 2015-12-01