Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name Hatena::Bookmark - Favorite Users Ratio
// @namespace http://d.hatena.ne.jp/KGA/
// @include http://b.hatena.ne.jp/entry/*
// ==/UserScript==
(function() {
var th = $X('//th[contains(., "お気に入り")]')[0];
if (!th) return;
var favs = $X('count(./following-sibling::td/ul[@class="userlist"]/li)', th, null, XPathResult.NUMBER_TYPE).wrappedJSObject.numberValue;
var users = $X('id("entry-info")/li[@class="users"]//a/text()', null, null, XPathResult.STRING_TYPE).wrappedJSObject.stringValue;
if (!users) return;
var count = parseInt(users.match(/\d+/)[0]);
var ratio = parseInt(favs / count * 100);
th.appendChild(document.createElement('br'));
th.appendChild(document.createTextNode([ratio, '% (', favs, '/', count, ')'].join('')));
// very simple version of $X
// $X(exp);
// $X(exp, context);
// $X(exp, context, resolver);
// @source http://gist.github.com/29681.txt
function $X (exp, context, resolver, result_type) {
context || (context = document);
var Doc = context.ownerDocument || context;
var result = Doc.evaluate(exp, context, resolver, result_type || XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (result_type) return result;
for (var i = 0, len = result.snapshotLength, res = []; i < len; i++) {
res.push(result.snapshotItem(i));
}
return res;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.