Skip to content

Instantly share code, notes, and snippets.

@motemen
Created March 28, 2014 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motemen/9825680 to your computer and use it in GitHub Desktop.
Save motemen/9825680 to your computer and use it in GitHub Desktop.
"Hatena Bookmark Count Favicon" userscript
// ==UserScript==
// @name Hatena Bookmark Count Favicon
// @namespace http://motemen.github.io/
// @match http://b.hatena.ne.jp/entry/*
// @match http://api.b.st-hatena.com/*
// ==/UserScript==
(function () {
var style = window.getComputedStyle(document.querySelector('ul.entry-data li.category a'));
function renderCount (count) {
var SIZE = 32;
var canvas = document.createElement('canvas');
canvas.setAttribute('width', SIZE);
canvas.setAttribute('height', SIZE);
var context = canvas.getContext('2d');
context.font = (SIZE / Math.sqrt(count.toString().length + 1)) + 'px Helvetica, sans-serif';
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillStyle = style.backgroundColor;
context.fillRect(0, 0, SIZE, SIZE);
context.fillStyle = style.color;
context.fillText(count.toString(), SIZE / 2, SIZE / 2);
var previousIcons = document.querySelectorAll('link[rel$=icon]');
for (var i = 0; i < previousIcons.length; i++) {
previousIcons[i].parentNode.removeChild(previousIcons[i]);
}
var link = document.createElement('link');
link.setAttribute('type', 'image/x-icon');
link.setAttribute('rel', 'icon');
link.setAttribute('href', canvas.toDataURL('image/png'));
document.querySelector('head').appendChild(link);
}
var count = document.querySelector('ul.users span').innerText;
renderCount(count)
if (location.hash.match(/\bautoupdate(?:=(\d+))?\b/)) {
var interval = parseInt(RegExp.$1) || 10;
var url = document.querySelector('#head-entry-link').href;
setInterval(function () {
GM_xmlhttpRequest({
method: 'GET',
url: 'http://api.b.st-hatena.com/entry.count?url=' + encodeURIComponent(url),
onload: function (res) {
renderCount(res.responseText);
}
})
}, interval * 60 * 1000);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment