WebScrap

October 14, 2009

Googleの検索結果にはてなブックマークの被ブックマーク数を表示するGreasemonkey

SBMCountWithGoogleをはてなブックマークの被ブックマーク数だけ表示するように修正したもの。また、被ブックマーク数の見た目も調整。


// ==UserScript==
// @name           Hatena Bookmark Count With Google
// @namespace      http://other-webscrap.blogspot.com/2009/10/googlegreasemonkey_14.html
// @description    show SBM count in Google search result
// @include        http://*.google.*/search?q=*
// ==/UserScript==

(function (){

 var version = "2009.10.09";
 var author  = "shiro edited by other";

 var launch = function(){
  hatena();
 }


 var XPATH = "//a[@class='l']";

 String.prototype.htmlescape = function() {
  return this.replace(/&/g, "&").replace(/</G, ?<?).replace( />/g, ">");
 }

 function forEachMatch(xpath, cb, class){
  var matches = document.evaluate(
   xpath, document, null,
   XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  for (var i=0; i1?"s":"")));
   
   var img = document.createElement("img");
   img.setAttribute("src", icon);
   container.appendChild(img);
  }
  with(a.style){
   verticalAlign = "middle";
   fontSize = "0.8em";
   textDecoration = "none";
   fontWeight = "bold";
   borderBottom = "1px solid #ff0000";
   backgroundColor = "#ffcccc";
   color = "red";
  }
  container.appendChild(a);
  link.parentNode.insertBefore(container, link.nextSibling);
  link.parentNode.insertBefore(document.createTextNode(" "), link.nextSibling);
 }

 // hatena
 var hatena_icon = '';
 var hatena_loaded = {};
 var hatena = function() {
  function callXmlrpc(requestbody, url2link) {
   const endpoint = "http://b.hatena.ne.jp/xmlrpc";
   function onload(response) {
    var pattern = /([^<]+)<\/name>\s*(\d+)/g;
    var m;
    while (m = pattern.exec(response.responseText)) {
     var link = url2link[m[1]];
     if(link) setBookmarkCount(link, m[2], "http://b.hatena.ne.jp/entry/"+link.href, hatena_icon, "hatena");
    }
   }
   GM_xmlhttpRequest({method: "POST", url: endpoint, data: requestbody, onload: onload});
  }
  var request = '<?xml version="1.0"?>\n\nbookmark.getCount\n\n';
  var url2link = new Array();
  var cb = function(link){
   request += ""+link.href.htmlescape()+"\n";
   url2link[link.href] = link;
  }
  forEachMatch(XPATH, cb, "hatena");
  request += "\n\n";
  callXmlrpc(request, url2link);
 }

 // for debug
 function debug1(id){
  var div = document.createElement("div");
  div.setAttribute("id", id);
  var hash = eval(id + "_loaded");
  var lst = new Array();
  for(var key in hash) lst.push(key);
  var div1 = document.createElement("div");
  div1.appendChild(document.createElement("br"));
  div1.appendChild(document.createTextNode(">>> "+id+"( "+lst.length+" ):"));
  div.appendChild(div1);
  var txt = document.createTextNode(lst.join(", "));
  div.appendChild(txt);
  div.appendChild(document.createElement("br"));
  var res = document.getElementById("res");
  res.parentNode.insertBefore(div,res);
 }
 function debug(){
  var bmlst = ["hatena"];
  for(var i=0; i 100){
//    debug();
    scrollHeight = document.documentElement.scrollHeight;
    launch();
   }
  },false);

 launch();
})();

0 Coment:

Post a Comment