AutoPagerize darekagakaku

    @@ -76,7 +76,11 @@ } target_rewrite(docs); docs = docs.map(function(doc) { - return insert_parent.appendChild(document.importNode(doc, true)); + insert_parent.appendChild(document.importNode(doc, true)); + if (document.querySelector('.content').offsetHeight < window.innerHeight) { + next_page_load(); + } + return true; }); return loading = false; }
  • /*
     * @title AutoPagerize darekagakaku
     * @description 自分が書かなければおそらく誰かが書く日記 (http://darekagakaku.herokuapp.com/) 用のAutoPagerize
     * @include http://darekagakaku.herokuapp.com/v/*
     * @license reblog commons license ( cc by 3.0 )
     */
    
    (function() {
        var start_url = location.href;
    
        var State = true;
        var BASE_REMAIN_HEIGHT = 10000;
        var loading = false;
    
        var last_element = (function() {
            var a = document.querySelector('.content');
            return a.childNodes[a.childElementCount - 2];
        }());
    
        var origin = Date.parse('2012-02-17');
    
        if (!last_element || !origin) {
            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(location.href);
            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) {
                insert_parent.appendChild(document.importNode(doc, true));
                if (document.querySelector('.content').offsetHeight < window.innerHeight) {
                    next_page_load();
                }
                return true;
            });
            return loading = false;
        }
    
        function get_next_elements(doc) {
            var content = doc.querySelector('.content');
            content.removeChild(content.lastChild);
            return Array.apply(null, content.childNodes);
        }
    
        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_next(url) {
            var date = url.slice(-10);
            time = Date.parse(date);
            time -= 86400000;
            if (time < origin) {
                return error();
            }
            var yesterday = new Date(time).toISOString().slice(0,10);
            var x = '/v/' + yesterday;
            return x;
        }
    
    return true;
    })();
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2014/05/14 18:52:03 - 2014-05-14
  2. 2014/05/13 14:48:18 - 2014-05-13
  3. 2014/05/12 12:25:00 - 2014-05-12
  4. 2014/05/11 03:37:20 - 2014-05-11
  5. 2014/05/11 03:22:46 - 2014-05-11
  6. 2014/05/11 03:20:28 - 2014-05-11
  7. 2014/05/11 03:20:05 - 2014-05-11