Hatena::Let Omikuji
by
noromanba
2016-01-11 [2016/01/11 03:15:32]
Hatena::Let Fortune cookie
@@ -5,10 +5,6 @@
* @license MIT License http://nrm.mit-license.org/2012
* @require
*/
-// TBD <at>require JSDeferred
-
-// Ten.XHR() 2nd args like this: { data: { 'Content-Type' : 'application/xml', 'Accept' : 'application/xml'} }
-// expected responseXML, but actual response is 'Content-Type:text/html; charset=utf-8'
// TODO feedback indicator
(function () {
@@ -22,27 +18,34 @@
};
// c.f. http://cho45.stfuawsc.com/jsdeferred/
- // http://cho45.stfuawsc.com/jsdeferred/doc/Deferred.html
+ // http://cho45.stfuawsc.com/jsdeferred/doc/
+ // http://gihyo.jp/dev/feature/01/jsdeferred/
+ // http://d.hatena.ne.jp/edvakf/20090414/1239726515
+
var total;
- Ten.Deferred.define();
- //Ten.Deferred().
- next(function () {
+ Ten.Deferred. // if you want to use 'Deferred' in globaly, replace to 'Ten.Deferred.define()'
+ next(function (D) {
new Ten.XHR('http://let.hatelabo.jp/', null, function (req) {
var countSelector = /<span\sclass=\"top-bookmarklet-count\">\s+(\d+)\sBookmarklets!\s+<\/span>/;
total = (countSelector.exec(req.responseText) || [])[1];
});
- }).wait(1). // FIXME omit wait(), assert 'total'
- next(function () {
- // http://let.hatelabo.jp/?of= (offset: 0 origin)
+ }).
+ // XXX 1st and 2nd XHR are async. needs to be create new D and return D-wrapped func inside next() callback
+ // i.e. if 1st responce time approx over 1sec(1000ms), 2nd XHR fail. why? the 'count' is undefined yet
+ // possible cause when networks too slow
+ wait(1). // stopgap
+ next(function (D) {
+ // http://let.hatelabo.jp/?of=<offset> offset: 0 origin
new Ten.XHR('http://let.hatelabo.jp/?of=' + random(total - 1), null, function (req) {
var codeSelector = /<a\shref\="(\/[\w-]+\/let\/[\w-]+)"/g;
var code, codes = [];
while ((code = codeSelector.exec(req.responseText))) {
- codes.push(code[1]); // captured group, absolutely
+ codes.push(code[1]); // absolutely refer captured-group
}
if (codes.length < 1) return;
location.pathname = codes[random(codes.length - 1)];
+ //console.info('*', codes[random(codes.length - 1)]);
});
}).
error(function (e) {
/*
* @title Hatena::Let::Omikuji
* @description Hatena::Let Fortune cookie
* @include http://let.hatelabo.jp/*
* @license MIT License http://nrm.mit-license.org/2012
* @require
*/
// TODO feedback indicator
(function () {
if (location.hostname !== 'let.hatelabo.jp') {
location.href = 'http://let.hatelabo.jp';
return; // for Fx
}
var random = function (max) {
return Math.floor(Math.random() * Number(max) + 1);
};
// c.f. http://cho45.stfuawsc.com/jsdeferred/
// http://cho45.stfuawsc.com/jsdeferred/doc/
// http://gihyo.jp/dev/feature/01/jsdeferred/
// http://d.hatena.ne.jp/edvakf/20090414/1239726515
var total;
Ten.Deferred. // if you want to use 'Deferred' in globaly, replace to 'Ten.Deferred.define()'
next(function (D) {
new Ten.XHR('http://let.hatelabo.jp/', null, function (req) {
var countSelector = /<span\sclass=\"top-bookmarklet-count\">\s+(\d+)\sBookmarklets!\s+<\/span>/;
total = (countSelector.exec(req.responseText) || [])[1];
});
}).
// XXX 1st and 2nd XHR are async. needs to be create new D and return D-wrapped func inside next() callback
// i.e. if 1st responce time approx over 1sec(1000ms), 2nd XHR fail. why? the 'count' is undefined yet
// possible cause when networks too slow
wait(1). // stopgap
next(function (D) {
// http://let.hatelabo.jp/?of=<offset> offset: 0 origin
new Ten.XHR('http://let.hatelabo.jp/?of=' + random(total - 1), null, function (req) {
var codeSelector = /<a\shref\="(\/[\w-]+\/let\/[\w-]+)"/g;
var code, codes = [];
while ((code = codeSelector.exec(req.responseText))) {
codes.push(code[1]); // absolutely refer captured-group
}
if (codes.length < 1) return;
location.pathname = codes[random(codes.length - 1)];
//console.info('*', codes[random(codes.length - 1)]);
});
}).
error(function (e) {
if (console && console.warn) {
console.warn('*error caught by Hatena::Let::Omikuji =>', e);
}
});
})();
- Permalink
- このページへの個別リンクです。
- RAW
- 書かれたコードへの直接のリンクです。
- Packed
- 文字列が圧縮された書かれたコードへのリンクです。
- Userscript
- Greasemonkey 等で利用する場合の .user.js へのリンクです。
- Loader
- @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
- Metadata
- コード中にコメントで @xxx と書かれたメタデータの JSON です。