wikipedia-cutup

    @@ -1,4 +1,3 @@ - /* * @title wikipedia-cutup * @description Wikipediaの記事をカットアップします @@ -57,7 +56,7 @@ clearInterval(timer); - var fragment = document.createFragment(); + var fragment = document.createDocumentFragment(); for (i = 0; i < 100; i++) { var page = selectRandom(pages); fragment.appendChild(page.getNode());
  • /*
     * @title wikipedia-cutup
     * @description Wikipediaの記事をカットアップします
     * @include http://ja.wikipedia.org/wiki/
     * @license MIT License
     */
    
    function selectRandom(list) {
        return list[Math.floor(Math.random() * list.length)];
    };
    
    function Page(url) {
        var self = this;
        $.ajax({
            url: url || 'http://ja.wikipedia.org/wiki/Special:Randompage',
            dataType: 'html'
        }).success(function(res) {
            self.nodes = $(res).find('.mw-content-ltr>*');
            self.title = $(res).find('h1').text();
            self.success = true;
        });
    }
    
    Page.prototype = {
        getNode: function() {
            if (!this.nodes) return null;
            return $(selectRandom(this.nodes)).clone();
        }
    };
    
    var i = 0;
    var pages = [];
    pages.push(new Page(location.href));
    
    for (i = 0; i < 5; i++) {
        pages.push(new Page);
    }
    
    $('h1').text('...');
    
    var timer = setInterval(function() {
        var i;
        for (i = 0; i < pages.length; i++) {
            if (!pages[i].success) {
                return;
            }
        }
    
        $('#bodyContent').empty();
    
        var titles = [];
        for (i = 0; i < pages.length; i++) {
            titles.push(pages[i].title);
        }
        $('h1').text(titles.join(' '));
    
        clearInterval(timer);
    
        var fragment = document.createDocumentFragment();
        for (i = 0; i < 100; i++) {
            var page = selectRandom(pages);
            fragment.appendChild(page.getNode());
        }
        $('#bodyContent').append(fragment);
    }, 100);
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2011/12/07 16:28:38 - 2011-12-07
  2. 2011/12/07 16:24:32 - 2011-12-07
  3. 2011/12/07 16:22:56 - 2011-12-07
  4. 2011/12/07 16:22:01 - 2011-12-07
  5. 2011/12/07 16:11:56 - 2011-12-07
  6. 2011/12/07 16:09:17 - 2011-12-07