Everlasting Juick

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

当前为 2014-07-01 提交的版本,查看 最新版本

  1. // Generated by CoffeeScript 1.7.1
  2.  
  3. // ==UserScript==
  4. // @name Everlasting Juick
  5. // @namespace http://dluciv.name/
  6. // @description Everlasting Juick scrolling / Бесконечный скроллинг Жуйка
  7. // @author dluciv
  8. // @license WTFPLv2 (http://wtfpl.net/)
  9. // @version 0.0.2.9
  10. // @icon https://raw.githubusercontent.com/dluciv/UserScripts/master/juick.com/everlasting-juick.png
  11. // @homepage https://github.com/dluciv/UserScripts/tree/master/juick.com
  12. // @grant none
  13. //
  14. // @include http://juick.com/*
  15. // ==/UserScript==
  16. ;
  17. window.addEventListener('load', function() {
  18. var $, addClickHandlers, bottomScrollArea, doNotLoadNext, hash, initialbefore, loadNextPage, locWithoutHash, locWithoutHashSearch, newer_page, prevpage, scrollto, scrolly, search;
  19. $ = unsafeWindow.jQuery;
  20. bottomScrollArea = 350;
  21. doNotLoadNext = false;
  22. locWithoutHashSearch = function(loc) {
  23. return loc.protocol + '//' + loc.hostname + (loc.port ? ":" + loc.port : "") + loc.pathname;
  24. };
  25. locWithoutHash = function(loc) {
  26. return loc.protocol + '//' + loc.hostname + (loc.port ? ":" + loc.port : "") + loc.pathname + (loc.search || "");
  27. };
  28. addClickHandlers = function(selector) {
  29. return selector.click(function(e) {
  30. var elt, loadedfrom, msg, msgy, pageid, query, where2return;
  31. elt = $(e.target);
  32. msg = elt.closest('article');
  33. pageid = msg.closest('div.everlasting-div').attr('id');
  34. query = '';
  35. loadedfrom = pageid.match(/loaded_from_(\d+)/);
  36. if (loadedfrom && +loadedfrom[1]) {
  37. query = '?before=' + loadedfrom[1];
  38. }
  39. msgy = msg.offset().top - $(window).scrollTop();
  40. where2return = locWithoutHashSearch(window.location) + query + "#" + msg.attr('data-mid') + '@' + msgy;
  41. window.history.replaceState({}, window.title, where2return);
  42. return console.log("Will return to " + where2return);
  43. });
  44. };
  45. prevpage = '';
  46. loadNextPage = function() {
  47. var oldprevpage, prevpagenum;
  48. doNotLoadNext = true;
  49. oldprevpage = prevpage;
  50. prevpage = $('p.page:last a').attr('href');
  51. $('p.page:last').remove();
  52. if (prevpage) {
  53. prevpagenum = prevpage.match(/.*\?before=(\d+).*/)[1];
  54. console.log("Loading new page: " + prevpage + " (# " + prevpagenum + ")...");
  55.  
  56. /*
  57. * $.ajax then parse HTML fails for Juick, so playing dirty.
  58. * Seems like browser sets innerHTML and then repairs markup,
  59. * so below approach works.
  60. */
  61. $('body > section#content > div.everlasting-div:last').after("<div class=\"everlasting-div\" id=\"loaded_from_" + prevpagenum + "\"></div>");
  62. return $("div#loaded_from_" + prevpagenum).load(prevpage + ' #content', function(data, status, req) {
  63. var newer_pages;
  64. console.log("Status: " + status);
  65. if (status === 'success') {
  66. $("div#loaded_from_" + prevpagenum + " article.ads").remove();
  67. $("div#loaded_from_" + prevpagenum + " div[id^=\"yandex_ad_\"]").remove();
  68. $("div#loaded_from_" + prevpagenum + " article").unwrap();
  69. addClickHandlers($("div#loaded_from_" + prevpagenum + " > article a"));
  70. newer_pages = JSON.parse(sessionStorage["newer_pages"] || "{}");
  71. newer_pages[prevpagenum] = oldprevpage;
  72. sessionStorage["newer_pages"] = JSON.stringify(newer_pages);
  73. return setTimeout(function() {
  74. return doNotLoadNext = false;
  75. }, 500);
  76. }
  77. });
  78. } else {
  79. return console.log("Likely reached the very bottom!..");
  80. }
  81. };
  82. if (!window.location.pathname.match(/[a-zA-Z0-9]+\/[0-9]+/)) {
  83. $('html body div#footer').css({
  84. position: "fixed",
  85. bottom: "5px",
  86. left: "0px",
  87. 'background-color': "#f7f7f7",
  88. opacity: "0.8",
  89. padding: "5px",
  90. 'border-radius': "5px"
  91. });
  92. $('section#content').after('<div style="height: 40px;"/>');
  93. }
  94. $('article.ads, div#footer-left, div[id^="yandex_ad_"]').remove();
  95. initialbefore = window.location.search.match(/\?before=(\d+).*/);
  96. initialbefore = initialbefore && initialbefore[1] || "0";
  97. console.log("Initial before = " + initialbefore);
  98. $('body section#content > article').wrapAll("<div class=\"everlasting-div\" id=\"loaded_from_" + initialbefore + "\"></div>");
  99. hash = window.location.hash.match(/(\d+)@(\d+)/);
  100. if (hash) {
  101. scrollto = hash[1];
  102. scrolly = hash[2];
  103. $(window).scrollTop($("article[data-mid=\"" + scrollto + "\"]").offset().top - (+scrolly));
  104. }
  105. addClickHandlers($("div#loaded_from_" + initialbefore + " > article a"));
  106. if (search = window.location.search.match(/\?before=(\d+)/)) {
  107. if (newer_page = JSON.parse(sessionStorage["newer_pages"] || "{}")[search[1]]) {
  108. $("section#content").prepend("<p><a href=\"" + newer_page + "\">&lt;&lt; Newer</a></p>");
  109. }
  110. }
  111. $(window).scroll(function(evt) {
  112. if (!doNotLoadNext && $(window).innerHeight() + $(window).scrollTop() >= $("body").height() - bottomScrollArea) {
  113. return loadNextPage();
  114. }
  115. });
  116. });