Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saitamanodoruji/5726562 to your computer and use it in GitHub Desktop.
Save saitamanodoruji/5726562 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name replace star by profile icon
// @namespace https://www.hatena.ne.jp/noromanba/
// @description replace star by profile icon for UserScript
// @include http://*
// @include https://*.hatena.tld/*
// @exclude http://serif.hatelabo.jp/*
// @version 2013.6.7.1
// @homepage https://gist.github.com/2725191
// @downloadURL https://gist.github.com/saitamanodoruji/5726562/raw/replaceStarByProfileIcon.user.js
// @installURL https://gist.github.com/saitamanodoruji/5726562/raw/replaceStarByProfileIcon.user.js
// @contributor os0x https://gist.github.com/2424/
// @contributor nikolat https://gist.github.com/761858/
// @contributor saitamanodoruji https://gist.github.com/saitamanodoruji/5726562
// @author noromanba (https://www.hatena.ne.jp/noromanba/)
// @license Unknown (as-is)
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/Star_coin.png/32px-Star_coin.png
// @icon64 https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/Star_coin.png/64px-Star_coin.png
// ==/UserScript==
// Icon (Public Domain by Mr.Yahoo!)
// https://commons.wikimedia.org/wiki/File:Star_coin.png
// c.f. http://d.hatena.ne.jp/os0x/20070911/1189544433
(function () {
// use native avatarize on Hatena Blog
if (document.querySelector('iframe#globalheader') && !(/^\/archive/.test(location.pathname))) {
// TBD check exist Hatena.Star
return;
}
var executeBrowserContext = function (funcOrString) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.charset = 'utf-8';
s.appendChild(document.createTextNode('(' + funcOrString.toString() + ')();'));
(document.head || document.body).appendChild(s);
};
// NIY generalize
var times = function (fn, condition) {
var timer, interval = 60,
faild = 0, threshold = 5;
timer = setInterval(function () {
if (condition) {
fn();
clearInterval(timer);
}
faild += 1;
console.info('*faild:', faild);
if (faild > threshold) {
console.info('*out of bounds');
clearInterval(timer);
}
}, interval);
};
// TODO lazy replace e.g. setInterval x N
var avatarize = function () {
if (!window.Hatena || !window.Hatena.Star) return;
var coloring = (function () {
// You can change color as you like. e.g. 'green' : 'rgb(0, 255, 0)', 'red' : '#f30', ...
var palette = {
'yellow': '#ffd700',
'green' : 'green',
'red' : 'red',
'blue' : 'blue',
'purple': 'purple',
'temp' : 'ghostwhite'
};
return function (color) {
// If You want coloring yellow star, remove '/*' and '*/' (yellow => undefined)
return '2px solid ' + (palette[color] || color /*|| palette['yellow']*/);
};
})();
Array.prototype.forEach.call(document.querySelectorAll('span.hatena-star-star-container > a'), function (star) {
if (/profile-icon/.test(star.className)) return;
if (/^[a-zA-Z][-\w]{1,30}[a-zA-Z\d]/.test(star.alt)) {
var color = (/star-(\w+)\.gif$/.exec(star.src) || [])[1];
star.style.border = coloring(color);
star.src = Hatena.User.getProfileIcon(star.alt).src;
}
// else if (false) {
// // TODO handle Twitter(or FB) login user
// }
});
var pushStars = Hatena.Star.Entry.prototype.pushStars;
Hatena.Star.Entry.prototype.pushStars = function (stars, color) {
stars = stars.map(function (star) {
var image = Hatena.User.getProfileIcon(star.name);
image.alt = star.name;
image.title = ''; // block a tooltip cover-over popup
image.style.outline = coloring(color);
image.className = 'hatena-star-star';
star.img = image;
return star;
});
pushStars.call(this, stars, color);
};
var showName = Hatena.Star.Star.prototype.showName;
Hatena.Star.Star.prototype.showName = function (e) {
this.screen_name = this.name;
showName.call(this, e);
};
};
// c.f. http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#current-document-readiness
// http://javascripter.github.com/readystate.html
if (document.readyState === 'loading' || document.readyState === 'interactive') {
// TODO unworks bind to window.onload in Firefox
//window.addEventListener('load', function (evt) {
window.addEventListener('DOMContentLoaded', function (evt) {
executeBrowserContext(avatarize);
}, false);
} else {
executeBrowserContext(avatarize);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment