/*
* @title Hatena::Let::Omikuji
* @description コードおみくじ in Hatena::Let -> だいたい大吉 -> しあわせ(。◠‿◠。) ご利益: コード力アップ ◆ 連続サーフィンでさらにご利益アップ
* @include http://let.hatelabo.jp/*
* @license MIT License http://nrm.mit-license.org/2012
* @require
* @see https://en.wikipedia.org/wiki/Omikuji
* @see https://en.wikipedia.org/wiki/Fortune_cookie
* @see http://liosk.blog103.fc2.com/blog-entry-34.html
* @see http://d.hatena.ne.jp/javascripter/20080709/1215597363
*/
// Ten workout
// http://www.hatena.ne.jp/js/Ten.js
// http://s.hatena.ne.jp/js/HatenaStar.js
// http://let.hatelabo.jp/?of= (0 origin)
//
// Ten.XHR('', {}, callback)
// 2nd arg -> { data: { 'Content-Type' : 'application/xml', 'Accept' : 'application/xml'} }
// for responseXML. But actual response: 'Content-Type:text/html; charset=utf-8'.
(function () {
if (location.hostname === 'let.hatelabo.jp') {
if (!location.pathname.split('/')[1]) {
feelin(selectCounts());
}
new Ten.XHR('http://let.hatelabo.jp/', null, function(req) {
// XXX Ten.DOM.createElementFromString(), createDocumentFragment(), createContextualFragment()
var frag = document.createElement('div');
frag.innerHTML = req.responseText;
feelin(selectCounts(frag));
});
}
})();
function feelin(counts) {
var offset = random(counts - 1);
new Ten.XHR('http://let.hatelabo.jp/?of=' + offset, null, function(req) {
var frag = document.createElement('div');
frag.innerHTML = req.responseText;
var lets = Ten.querySelectorAll('.code-path', frag);
var feelin = lets[random(lets.length - 1)].href;
//console.info('::Let:: ' + 'feelin: ' + feelin);
location.href = feelin;
});
}
function selectCounts(node) {
var elm = Ten.querySelector('.top-bookmarklet-count', node);
var counts = elm.innerHTML.replace(/\s/g, '').replace(/(\d+)\D+/, '$1');
//console.info('::Let:: ' + 'counts: ' + counts);
return counts;
}
function random(num) {
return Math.floor(Math.random() * num + 1);
}