endless summer on anond

  • /*
     * @title endless summer on anond
     * @description random next link on anond
     * @include http://anond.hatelabo.jp/
     * @license reblog commons license ( cc by 3.0 )
     */
    
    /**
     * This script is forked from taizooo's "endless summer on dsbd"
     * http://let.hatelabo.jp/taizooo/let/gYC-xcz67_6bYQ
     */
    
    // bookmark
    // autopager part from "twitter.AutoPager" : http://userscripts.org/scripts/review/47998
    // random choice part from ReblogMachine : http://reblog.machine.mamemomonga.com/
    //   -> ReblogMachine.endless_summer#choice : http://reblog.machine.mamemomonga.com/js/ReblogMachine.js
    // history.pushState/history.replaceState : http://d.hatena.ne.jp/javascripter/20100404/1270411268
    // show page regexp from "Tumblr Dashboard Filter by Post Type" : http://userscripts.org/scripts/review/40794
    // Exponential Endless Summer version 2013.08 by saitamanodoruji : http://tumblr.g.hatena.ne.jp/saitamanodoruji/20130831/1377961157
    
    (function() {
        var start_url = location.href;
    
        var State = true;
        var BASE_REMAIN_HEIGHT = 10000;
        var loading = false;
    
        var last_element = (function() {
            var a = get_next_elements(document);
            return a[a.length - 1];
        }());
    
        var max_page_number = get_max_page_number(document);
    
        if (!last_element || !max_page_number) {
            return error();
        }
        var insert_point = last_element.nextSibling;
        var insert_parent = last_element.parentNode;
    
        next_page_load();
    
        window.addEventListener('scroll', function() {
            if (loading) return;
            var remain = document.documentElement.scrollHeight - window.innerHeight - window.pageYOffset;
            if (State && remain < BASE_REMAIN_HEIGHT) {
                next_page_load();
            }
        }, false);
    
        function next_page_load() {
            loading = true;
            var next = get_next(start_url);
            history.pushState("", "", next);
            console.log('next: ' + next);
            var x = new XMLHttpRequest();
            x.onload = function() {
                if (x.status <= 200 && x.status < 300) {
                    load(x);
                } else {
                    error();
                    next_page_load();
                }
            };
            x.open('GET', next, true);
            x.send(null);
        }
    
        function load(x) {
            var html = x.responseText.replace(/<script(?:[ \t\r\n][^>]*)?>[\S\s]*?<\/script[ \t\r\n]*>|<\/?(?:i?frame|html|script|object)(?:[ \t\r\n][^<>]*)?>/gi, ' ');
            var htmlDoc;
            if (document.implementation.createHTMLDocument) {
                htmlDoc = document.implementation.createHTMLDocument('hogehoge');
            } else {
                htmlDoc = document.cloneNode(false);
                if (htmlDoc) {
                    htmlDoc.appendChild(htmlDoc.importNode(document.documentElement, false));
                } else {
                    htmlDoc = document.implementation.createDocument(null, 'html', null);
                }
            }
            var range = document.createRange();
            range.selectNodeContents(document.documentElement);
            htmlDoc.documentElement.appendChild(range.createContextualFragment(html));
            var docs = get_next_elements(htmlDoc);
            if (!docs.length) {
                error();
                return next_page_load();
            }
            target_rewrite(docs);
            docs = docs.map(function(doc) {
                return insert_parent.appendChild(document.importNode(doc, true));
            });
            return loading = false;
        }
    
        function get_next_elements(doc) {
            return Array.apply(null, doc.querySelectorAll('.section'));
        }
    
        function target_rewrite(docs) {
            docs.forEach(function(doc) {
                var as = doc.getElementsByTagName('a');
                for (var i = 0, l = as.length; i < l; i++) {
                    var a = as[i],
                        _a = a.getAttribute('href');
                    if (_a && !/^javascript:/.test(_a) && !/^#/.test(_a)) a.setAttribute('target', '_blank');
                }
            });
        }
    
        function error() {
            return console.log('error');
            State = false;
        }
    
        function terminate() {
            return console.log('terminate');
            State = false;
        }
    
        function get_latest_id(doc) {
            return doc.evaluate('number(substring-after(//*[@class="section"]/h3/a/@href, "/"))', doc, null, XPathResult.NUMBER_TYPE, null).numberValue;
        }
    
        function get_next(url) {
            var random_id = Math.floor(Math.random() * max_page_number + 1);
            if (isNaN(random_id)) {
                return error();
            }
            var x = '/' + '?page=' + random_id;
            return x;
        }
    
        function get_max_page_number(doc) {
            return doc.evaluate('number(substring-before(//*[@class="pager-r"]/text(), "ページ中"))', doc, null, XPathResult.NUMBER_TYPE, null).numberValue;
        }
    return true;
    })();
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2014/05/11 03:19:27 - 2014-05-11
  2. 2014/05/11 03:17:38 - 2014-05-11
  3. 2014/01/07 21:06:15 - 2014-01-07
  4. 2014/01/07 15:31:13 - 2014-01-07
  5. 2014/01/07 12:21:32 - 2014-01-07