/*
* @title ☆button on anond
* @description はてな匿名ダイアリーに☆ボタン出します (for userscript)
* @include http://anond.hatelabo.jp*
* @license MIT License
* @require
*/
(function() {
function main () {
var iconizing = false;
var setupStar = function() {
var style = document.createElement('style');
style.innerHTML = '.hatena-star-add-button-image{background:url(http://hatenablog.com/images/theme/star/hatena-star-add-button.png);}';
document.querySelector('body').appendChild(style);
if (!Hatena.Star)
return;
var entryConfig = {
entryNodes: {
// PC, article
'div.star-target': {
uri: 'h3:first-child a:first-child',
title: 'h3:first-child',
container: 'h3:first-child'
}
}
};
Hatena.Star.SiteConfig = entryConfig;
};
setupSection = function() {
var sections = document.querySelectorAll('.section');
for (var i = 0, length = sections.length; i < length; i++) {
if (!sections[i].querySelector('.hatena-star-star-container')) {
sections[i].setAttribute('class', sections[i].getAttribute('class') + ' star-target');
}
}
};
var iconize = function () {
iconizing = true;
var containers = document.querySelectorAll('.hatena-star-star-container');
for (var i = 0, clength = containers.length; i < clength; i++) {
var as = containers[i].querySelectorAll('a');
for (var j = 0, alength = as.length; j < alength; j++) {
if (as[j].hasAttribute('iconized')) continue;
var user = as[j].href.replace('http://anond.hatelabo.jp/', '').replace('/', '');
var icon = document.createElement('img');
icon.src = 'http://cdn1.www.st-hatena.com/users/' + user.substr(0, 2) + '/' + user + '/profile.gif';
icon.setAttribute('style', 'height:20px;position:absolute;width:20px;');
var star = as[j].querySelector('.hatena-star-star');
star.style['position'] = 'absolute';
star.style['bottom'] = '1px';
star.style['width'] = '10px';
as[j].insertBefore(icon, star);
as[j].setAttribute('iconized', '');
as[j].style['width'] = '20px';
as[j].style['height'] = '20px';
as[j].style['position'] = 'relative';
as[j].style['display'] = 'inline-block';
as[j].style['margin'] = '0 1px 0 1px';
}
var section = containers[i].parentNode.parentNode;
section.setAttribute('class', section.getAttribute('class').replace('star-target', ''));
iconizing = false;
}
}
var startPolling = function () {
var timer = 0;
document.addEventListener('DOMNodeInserted', function() {
if(timer) return;
timer = setTimeout(function() {
setupSection();
iconize();
if (!iconizing)Hatena.Star.EntryLoader();
timer = 0;
}, 500);
}, false);
}
var s = document.createElement('script');
s.src = 'http://s.hatena.ne.jp/js/HatenaStar.js';
s.type = 'text/javascript';
s.addEventListener('load', function(){
setupStar();
setupSection();
startPolling();
}, false);
document.querySelector('body').appendChild(s);
}
var script = document.createElement("script");
script.textContent = "(" + main.toString() + ")();";
document.body.appendChild(script);
}());