Endless Gamersky

Load more results automatically and endlessly.

当前为 2015-11-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Endless Gamersky
  3. // @description Load more results automatically and endlessly.
  4. // @author fluffy
  5. // @oujs:author fluffy
  6. // @namespace kitty_the_lost@sina.com
  7. // @homepageURL https://greasyfork.org/en/scripts/14183-endless-gamersky
  8. // @supportURL
  9. // @icon
  10. // @include http://*.gamersky.com/*
  11. // @run-at document-start
  12. // @grant GM_xmlhttpRequest
  13. // @version 0.0.6
  14. // ==/UserScript==
  15.  
  16. document.addEventListener('DOMContentLoaded', function () {
  17.  
  18. // NOTE: Options
  19. var request_pct = 10; // percentage of window height left on document to request next page, value must be between 0-1
  20. var event_type = "scroll"; // or "wheel"
  21. var on_page_refresh = 1;
  22. // 0: reload all previous pages requested
  23. // 1: load only the first page (prevent restoring the scroll position)
  24. // 2: load only the last page requested
  25.  
  26. var Mid2L_ctt = document.querySelector(".Mid2L_con");
  27. var old_scrollY = 0;
  28. var scroll_events = 0;
  29. var next_link = null;
  30. var cols = [];
  31. var request_offsetHeight = 0;
  32. var stop = false;
  33.  
  34. window.addEventListener(event_type, onScroll, false);
  35. window.addEventListener("beforeunload", function () {
  36. window.scrollTo(0, 0);
  37. }, false);
  38.  
  39. function requestNextPage(link) {
  40. console.log("request next");
  41. console.log(link);
  42. GM_xmlhttpRequest({
  43. method: "GET",
  44. url: link,
  45. onload: function (response) {
  46. var holder = document.createElement("div");
  47. holder.innerHTML = response.responseText;
  48.  
  49. if (holder.querySelector(".page_css>a:last-of-type").innerHTML!="下一页") {
  50. stop = true;
  51. }
  52.  
  53. next_link = holder.querySelector(".page_css>a:last-of-type").href;
  54.  
  55. var next_col = document.createElement("div");
  56. next_col.className = "EG_col";
  57. next_col.appendChild(holder.querySelector(".Mid2L_con"));
  58.  
  59. cols.push(next_col);
  60. console.log("Page no: " + cols.length);
  61. next_col.id = next_col.className + "_" + (cols.length - 1); // NOTE: add unique id for every new col
  62.  
  63. if (!Mid2L_ctt || cols.length === 1) // NOTE: needs to be rechecked on a state reset too, and late insertation of element on google instant
  64. Mid2L_ctt = document.querySelector(".Mid2L_con");
  65. Mid2L_ctt.appendChild(next_col);
  66. window.addEventListener(event_type, onScroll, false);
  67. }
  68. });
  69.  
  70. }
  71.  
  72. function onScroll(e) {
  73. if (stop) {
  74. window.removeEventListener(event_type, onScroll, false);
  75. return;
  76. }
  77. var y = window.scrollY;
  78. // if (scroll_events === 0) old_scrollY = y; // stops only if scroll position was on 2. page
  79. var delta = e.deltaY || y - old_scrollY; // NOTE: e.deltaY for "wheel" event
  80. if (delta > 0 && (window.innerHeight + y) >= (document.body.clientHeight - (window.innerHeight * request_pct))) {
  81. console.log("scroll end");
  82. window.removeEventListener(event_type, onScroll, false);
  83.  
  84. try {
  85. requestNextPage(next_link || document.querySelector(".page_css>a:last-of-type").href);
  86. } catch (err) {
  87. console.error(err.name + ": " + err.message);
  88. // NOTE: recovery unnecessary, input event handles it with reset on new search
  89. }
  90. }
  91. old_scrollY = y;
  92. scroll_events += 1;
  93. }
  94.  
  95. // NOTE: Resets the script state on a new search
  96. function reset() {
  97. if (input.value !== input_value) {
  98. input_value = input.value;
  99. window.scrollTo(0, 0);
  100. for (var i = 0; i < cols.length; i++)
  101. rcnt.removeChild(cols[i]);
  102. cols = [];
  103. next_link = null;
  104. old_scrollY = 0;
  105. scroll_events = 0;
  106. console.log("RESET");
  107. }
  108. }
  109.  
  110. console.log("eGamersky.js initialized");
  111. });
  112. console.log("eGamersky.js loaded");