darekagakaku endless scroll and pushstate

    
      
  • /*
     * @title darekagakaku endless scroll
     * @description darekagakaku nikki wo random ni autopagerize
     * @include http://darekagakaku.herokuapp.com/*
     * @license MIT License
     */
    
    // http://darekagakaku.herokuapp.com/ wo endless summer-ize!!!
    // via http://userscripts.org/scripts/review/47998
    
    
    (function (Ap) {
      Ap = {};
      Ap.next = '';
    
      var State = true;
      var HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';
      var BASE_REMAIN_HEIGHT = 50;
      var loading = false;
    
      var now = new Date();
      var bottom = new Date("Feburary 17, 2012"); /* http://darekagakaku.herokuapp.com/v/2012-02-17 */
    
      //var Ap.next = get_next(document);
      var last_element = get_next_elements(document).pop();
      //if (!Ap.next || !last_element) {
      if (!last_element) {
        return error();
      }
      insert_point = last_element.nextSibling;
      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;
        //Ap.next = next.getAttribute('href');
        Ap.next = go();
        history.pushState("", "",  Ap.next);
        console.log(Ap.next);
        var x = new XMLHttpRequest();
        x.onload = function () {
          if (x.status <= 200 && x.status < 300) {
            load(x);
          } else {
            error();
          }
        };
        x.open('GET', Ap.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));
        //Ap.next = get_next(htmlDoc);
        var docs = get_next_elements(htmlDoc);
        if (!docs.length) {
          return error();
        }
        docs = docs.map(function (doc) {
          return insert_parent.insertBefore(document.importNode(doc, true), insert_point);
        });
        loading = false;
        //  if (!next) {
        //    return terminate();
        //  }
      }
    
      //function get_next(doc) {
      //  return doc.evaluate('id("next_page_link")', doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
      //}
      function get_next_elements(doc) {
        var r = doc.evaluate('//div[@class="content"]', doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        for (var i = 0, l = r.snapshotLength, res = new Array(l); i < l; i++) res[i] = r.snapshotItem(i);
        return res;
      }
    
      function error() {
        return console.log('error');
        State = false;
      }
    
      function terminate() {
        return console.log('terminate');
        State = false;
      }
    
      // via http://let.hatelabo.jp/taizooo/let/gYC-xcG60rLuBA
      function trim(num, l) {
        if (num < 0) {
          num = -(num);
        }
        if (typeof (num) != 'string') {
          num = '' + num;
        }
        var s = l - num.length;
        while (s > 0) {
          num = '0' + num;
          --s;
        }
        return num;
      }
    
      function rday(max, min) {
        max = max.getTime();
        min = min.getTime();
        rd = new Date(Math.floor(Math.random() * (max - min) + min));
        d = trim(rd.getFullYear(), 4) + "-" + trim(rd.getMonth() + 1, 2) + "-" + trim(rd.getDate(), 2);
        return d;
      }
    
      function go() {
        url = "http://darekagakaku.herokuapp.com/v/" + rday(now, bottom);
        if (Ap.next == url) {
          url = "http://darekagakaku.herokuapp.com/a";
        }
        return url;
      }
    
    })();
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2015/02/26 19:18:54 - 2015-02-26
  2. 2012/04/08 00:20:19 - 2012-04-08
  3. 2012/04/08 00:15:30 - 2012-04-08
  4. 2012/04/08 00:13:04 - 2012-04-08
  5. 2012/04/08 00:11:06 - 2012-04-08