Hatena::Let Omikuji

    @@ -3,43 +3,51 @@ * @description Hatena::Let Fortune cookie * @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 + * @require */ -// # NOTE -// Ten.XHR() 2nd args like this { data: { 'Content-Type' : 'application/xml', 'Accept' : 'application/xml'} } -// expected responseXML, but actual response: 'Content-Type:text/html; charset=utf-8' -// http://liosk.blog103.fc2.com/blog-entry-34.html +// 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 () { - if (location.hostname !== 'let.hatelabo.jp') location.href = '//let.hatelabo.jp'; + if (location.hostname !== 'let.hatelabo.jp') { + location.href = 'http://let.hatelabo.jp'; + return; // for Fx + } - new Ten.XHR('http://let.hatelabo.jp/', null, function(req) { - var countSelector = /<span\sclass=\"top-bookmarklet-count\">\s+(\d+)\sBookmarklets!\s+<\/span>/; - var total = countSelector.exec(req.responseText)[1] || []; - if (!total) return; - - oracle(total); - }); + var random = function (max) { + return Math.floor(Math.random() * Number(max) + 1); + }; - function oracle(counts) { - var offset = random(counts - 1); - // http://let.hatelabo.jp/?of= (0 origin paging) - new Ten.XHR('http://let.hatelabo.jp/?of=' + offset, null, function(req) { - var repoSelector = /<a\shref\="(\/[\w-]+\/let\/[\w-]+)"/g; - var repo, stack =[]; - while ((repo = repoSelector.exec(req.responseText))) { - stack.push(repo[1]); // push captuerd group + // c.f. http://cho45.stfuawsc.com/jsdeferred/ + // http://cho45.stfuawsc.com/jsdeferred/doc/Deferred.html + var total; + Ten.Deferred.define(); + //Ten.Deferred(). + next(function () { + 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) + 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 } - if (!stack || stack.length < 1) return; + if (codes.length < 1) return; - location.pathname = stack[random(stack.length - 1)]; + location.pathname = codes[random(codes.length - 1)]; }); - } - - function random(num) { - return Math.floor(Math.random() * Number(num) + 1); - } + }). + error(function (e) { + if (console && console.warn) { + console.warn('*error caught by Hatena::Let::Omikuji =>', 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
     */
    // 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 () {
        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/Deferred.html
        var total;
        Ten.Deferred.define();
        //Ten.Deferred().
        next(function () {
            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)
            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
                }
                if (codes.length < 1) return;
    
                location.pathname = 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 です。

History

  1. 2016/01/11 03:15:32 - 2016-01-11
  2. 2013/04/01 12:28:12 - 2013-04-01
  3. 2012/12/27 19:22:59 - 2012-12-27
  4. 2012/12/06 05:45:45 - 2012-12-06
  5. 2012/12/06 05:38:35 - 2012-12-06
  6. 2012/07/25 06:05:17 - 2012-07-25
  7. 2012/07/25 06:03:48 - 2012-07-25
  8. 2012/06/03 09:27:15 - 2012-06-03
  9. 2012/02/23 17:46:18 - 2012-02-23