darekagakaku endless scroll

    @@ -25,6 +25,8 @@ 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;
  • /*
     * @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
    
    var State = true;
    var HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';
    var BASE_REMAIN_HEIGHT = 4000;
    var loading = false;
    
    var now = new Date();
    var bottom = new Date("Feburary 17, 2012"); /* http://darekagakaku.herokuapp.com/v/2012-02-17 */
    
    //var next = get_next(document);
    var last_element = get_next_elements(document).pop();
    //if (!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) {
        console.log('fire');
        next_page_load();
      }
    }, false);
    
    function next_page_load() {
      loading = true;
      //var url = next.getAttribute('href');
      var url = "http://darekagakaku.herokuapp.com/v/" + rday(now, bottom);
      var x = new XMLHttpRequest();
      x.onload = function () {
        if (x.status <= 200 && x.status < 300) {
          load(x);
        } else {
          error();
        }
      };
      x.open('GET', url, 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));
      //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;
    }
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2012/04/02 22:38:40 - 2012-04-02
  2. 2012/04/02 22:36:27 - 2012-04-02
  3. 2012/04/02 22:35:15 - 2012-04-02
  4. 2012/03/30 18:43:31 - 2012-03-30
  5. 2012/03/30 00:25:10 - 2012-03-30
  6. 2012/03/29 19:00:23 - 2012-03-29