Everlasting Juick

Everlasting Juick scrolling / Бесконечный скроллинг Жуйка

目前為 2014-07-01 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// Generated by CoffeeScript 1.7.1

// ==UserScript==
// @name          Everlasting Juick
// @namespace     http://dluciv.name/
// @description   Everlasting Juick scrolling / Бесконечный скроллинг Жуйка
// @author        dluciv
// @license       WTFPLv2 (http://wtfpl.net/)
// @version       0.0.2.8
// @icon          https://raw.githubusercontent.com/dluciv/UserScripts/master/juick.com/everlasting-juick.png
// @homepage      https://github.com/dluciv/UserScripts/tree/master/juick.com
//
// @include       http://juick.com/*
// ==/UserScript==
;
window.addEventListener('load', function() {
  var $, addClickHandlers, bottomScrollArea, doNotLoadNext, hash, initialbefore, loadNextPage, locWithoutHash, locWithoutHashSearch, newer_page, prevpage, scrollto, scrolly, search;
  $ = unsafeWindow.jQuery;
  bottomScrollArea = 350;
  doNotLoadNext = false;
  locWithoutHashSearch = function(loc) {
    return loc.protocol + '//' + loc.hostname + (loc.port ? ":" + loc.port : "") + loc.pathname;
  };
  locWithoutHash = function(loc) {
    return loc.protocol + '//' + loc.hostname + (loc.port ? ":" + loc.port : "") + loc.pathname + (loc.search || "");
  };
  addClickHandlers = function(selector) {
    return selector.click(function(e) {
      var elt, loadedfrom, msg, msgy, pageid, query, where2return;
      elt = $(e.target);
      msg = elt.closest('article');
      pageid = msg.closest('div.everlasting-div').attr('id');
      query = '';
      loadedfrom = pageid.match(/loaded_from_(\d+)/);
      if (loadedfrom && +loadedfrom[1]) {
        query = '?before=' + loadedfrom[1];
      }
      msgy = msg.offset().top - $(window).scrollTop();
      where2return = locWithoutHashSearch(window.location) + query + "#" + msg.attr('data-mid') + '@' + msgy;
      window.history.replaceState({}, window.title, where2return);
      return console.log("Will return to " + where2return);
    });
  };
  prevpage = '';
  loadNextPage = function() {
    var oldprevpage, prevpagenum;
    doNotLoadNext = true;
    oldprevpage = prevpage;
    prevpage = $('p.page:last a').attr('href');
    $('p.page:last').remove();
    if (prevpage) {
      prevpagenum = prevpage.match(/.*\?before=(\d+).*/)[1];
      console.log("Loading new page: " + prevpage + " (# " + prevpagenum + ")...");

      /*
       * $.ajax then parse HTML fails for Juick, so playing dirty.
       * Seems like browser sets innerHTML and then repairs markup,
       * so below approach works.
       */
      $('body > section#content > div.everlasting-div:last').after("<div class=\"everlasting-div\" id=\"loaded_from_" + prevpagenum + "\"></div>");
      return $("div#loaded_from_" + prevpagenum).load(prevpage + ' #content', function(data, status, req) {
        var newer_pages;
        console.log("Status: " + status);
        if (status === 'success') {
          $("div#loaded_from_" + prevpagenum + " article.ads").remove();
          $("div#loaded_from_" + prevpagenum + " div[id^=\"yandex_ad_\"]").remove();
          $("div#loaded_from_" + prevpagenum + " article").unwrap();
          addClickHandlers($("div#loaded_from_" + prevpagenum + " > article a"));
          newer_pages = JSON.parse(sessionStorage["newer_pages"] || "{}");
          newer_pages[prevpagenum] = oldprevpage;
          sessionStorage["newer_pages"] = JSON.stringify(newer_pages);
          return setTimeout(function() {
            return doNotLoadNext = false;
          }, 500);
        }
      });
    } else {
      return console.log("Likely reached the very bottom!..");
    }
  };
  if (!window.location.pathname.match(/[a-zA-Z0-9]+\/[0-9]+/)) {
    $('html body div#footer').css({
      position: "fixed",
      bottom: "5px",
      left: "0px",
      'background-color': "#f7f7f7",
      opacity: "0.8",
      padding: "5px",
      'border-radius': "5px"
    });
    $('section#content').after('<div style="height: 40px;"/>');
  }
  $('article.ads, div#footer-left,  div[id^="yandex_ad_"]').remove();
  initialbefore = window.location.search.match(/\?before=(\d+).*/);
  initialbefore = initialbefore && initialbefore[1] || "0";
  console.log("Initial before = " + initialbefore);
  $('body section#content > article').wrapAll("<div class=\"everlasting-div\" id=\"loaded_from_" + initialbefore + "\"></div>");
  hash = window.location.hash.match(/(\d+)@(\d+)/);
  if (hash) {
    scrollto = hash[1];
    scrolly = hash[2];
    $(window).scrollTop($("article[data-mid=\"" + scrollto + "\"]").offset().top - (+scrolly));
  }
  addClickHandlers($("div#loaded_from_" + initialbefore + " > article a"));
  if (search = window.location.search.match(/\?before=(\d+)/)) {
    if (newer_page = JSON.parse(sessionStorage["newer_pages"] || "{}")[search[1]]) {
      $("section#content").prepend("<p><a href=\"" + newer_page + "\">&lt;&lt; Newer</a></p>");
    }
  }
  $(window).scroll(function(evt) {
    if (!doNotLoadNext && $(window).innerHeight() + $(window).scrollTop() >= $("body").height() - bottomScrollArea) {
      return loadNextPage();
    }
  });
});