Skip to content

Instantly share code, notes, and snippets.

@kurumigi
Created July 6, 2009 07:35
Show Gist options
  • Save kurumigi/141317 to your computer and use it in GitHub Desktop.
Save kurumigi/141317 to your computer and use it in GitHub Desktop.
[GM script]Search engines+SBM
// ==UserScript==
// @name Search engines+SBM
// @namespace http://d.hatena.ne.jp/kurumigi/
// @description Show SBM count in Search engines.
// @include http://*
// ==/UserScript==
// this script based on "Google+SBM".
// http://wildlifesanctuary.blog38.fc2.com/blog-entry-141.html
// original script based on
// http://d.hatena.ne.jp/kusigahama/20051207#p1
// http://la.ma.la/blog/diary_200607281316.htm
(function () {
// === Configration ===
const USE_AUTOPAGER = true;
// SITEINFO of SBMs
var SBM_SITEINFO = [
{
name: 'hatena bookmark',
entryUrl: 'http://b.hatena.ne.jp/entry/',
imageUrl: 'http://b.hatena.ne.jp/entry/image/',
imageHeight: '13px',
use: true,
},
{
name: 'livedoor clip',
entryUrl: 'http://clip.livedoor.com/page/',
imageUrl: 'http://image.clip.livedoor.com/counter/',
imageHeight: '12px',
use: true,
},
{
name: 'Yahoo! bookmark (Japan)',
entryUrl: 'http://bookmarks.yahoo.co.jp/url/',
imageUrl: 'http://num.bookmarks.yahoo.co.jp/image/shortsmall/',
imageHeight: '13px',
use: true,
},
{
name: 'Buzzurl',
entryUrl: 'http://buzzurl.jp/entry/',
imageUrl: 'http://api.buzzurl.jp/api/counter/v1/image?url=',
imageHeight: '12px',
use: true,
},
{
name: 'POOKMARK Airlines',
entryUrl: 'http://pookmark.jp/url/',
imageUrl: 'http://pookmark.jp/count/',
imageHeight: '12px',
use: true,
},
]
// SITEINFO of search engines
var Search_SITEINFO = [
{
name: 'Google search',
url: /^http:\/\/[^.]+\.google\.(?:[^.]+\.)?[^./]+\/(?:search|custom|cse)/i,
xPath: './/a[@class="l"]',
use: true,
},
{
name: 'Google news',
url: /^http:\/\/news\.google\.co\.jp\//i,
xPath: './/h2[@class="title"]/a[starts-with(@class,"usg-")] | .//div[@class="additional-article"]/div/a[starts-with(@class,"usg-")]',
use: true,
},
{
name: 'Bing',
url: /^http:\/\/www\.bing\.com\/search/i,
xPath: './/div[@class="sb_tlst"]/h3/a',
use: true,
},
]
// === Global variables ===
// using xPath
var xPath;
// === Functions ===
function setBookmarkCounter(url) {
var a, img, sbm;
var url = url.replace(/#/, '%23');
var span = document.createElement('span');
span.setAttribute('class', 'sbm')
for (var i = 0; i < SBM_SITEINFO.length; i++) {
if (SBM_SITEINFO[i].use) {
sbm = SBM_SITEINFO[i];
a = document.createElement('a');
img = document.createElement('img');
a.setAttribute('title', sbm.name);
a.setAttribute('href', sbm.entryUrl + url);
img.setAttribute('src', sbm.imageUrl + url);
img.style.maxHeight = sbm.imageHeight;
a.appendChild(img);
span.appendChild(a);
}
}
return span;
}
function load(context) {
var context = context || [document];
for (var i = 0; i < context.length; i++) {
GM_log(context[i].nodeName);
var links = document.evaluate(xPath, context[i], null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if (links.snapshotLength) {
for (var j = 0; j < links.snapshotLength; j++) {
var link = links.snapshotItem(j);
link.parentNode.appendChild(setBookmarkCounter(link.href));
}
}
}
}
function addStyle() {
var style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode('span.sbm img { border: none; margin: 0 -2px 0 4px; vertical-align: text-bottom; }'));
document.getElementsByTagName('head')[0].appendChild(style);
}
function checkPage() {
for (var i = 0; i < Search_SITEINFO.length; i++) {
if (location.href.match(Search_SITEINFO[i]['url'])) {
xPath = Search_SITEINFO[i]['xPath'];
return true;
}
}
return false;
}
// === Main routine ===
// run only if an URL matched
if (checkPage()) {
addStyle();
load();
// for AutoPagerize
if (USE_AUTOPAGER) {
if (window.AutoPagerize.addFilter) {
window.AutoPagerize.addFilter(load);
} else {
window.addEventListener('GM_AutoPagerizeLoaded', load, false);
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment