Everlasting Juick

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

当前为 2014-06-30 提交的版本,查看 最新版本

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