/*
* @title Google検索の結果にFacebookのシェア数を表示するブックマークレット
* @description 同上
* @include https://www.google.*
* @license MIT License
*/
(function(document){
"use strict"
function queryIds(nodes, text) {
const n = nodes.pop();
if (nodes.length == 0) {
const t = text + n.href;
const q = t.replace(/&/g, '%26').replace(/=/g, '%3D').replace(/\?/g, '%3F');
return q;
}
const t = text + n.href + ',';
return queryIds(nodes, t);
}
function loadJSONP(url) {
const script = document.createElement('SCRIPT');
script.src = url;
document.body.insertBefore(script, null);
}
function insertCounter(info, i) {
const 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;";
const counter = document.createElement('SPAN');
counter.setAttribute('style', STYLE);
counter.dataset['id'] = info.id;
counter.innerText = info.shares || 0;
const parentNode = nodes[i].parentNode
parentNode.style.setProperty('overflow', 'visible');
parentNode.appendChild(counter);
}
window.callback = function(data){
Object.keys(data).map((k, i) => {
const info = data[k];
insertCounter(info, i);
})
};
const nodes = Array.from(document.querySelectorAll('h3.r > a[onmousedown]'));
const ids = queryIds(nodes.slice(), "");
loadJSONP('https://graph.facebook.com/?ids='+ids+'&callback=callback');
})(document);