Gelbooru Endless Scroll

Adds endless scroll function to Gelbooru

Verzia zo dňa 19.11.2016. Pozri najnovšiu verziu.

  1. // ==UserScript==
  2. // @id gelbooru-endless-scroll
  3. // @name Gelbooru Endless Scroll
  4. // @version 1.0.2
  5. // @namespace intermission
  6. // @author intermission
  7. // @license CC0; https://wiki.creativecommons.org/wiki/CC0
  8. // @description Adds endless scroll function to Gelbooru
  9. // @include http://gelbooru.com/index.php?*
  10. // @include https://gelbooru.com/index.php?*
  11. // @run-at document-end
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function(){
  16. var d = document, _on = false, test_var,
  17. vis = function(el) {
  18. var rect = el.getBoundingClientRect();
  19. return (
  20. rect.top + rect.height >= 0 &&
  21. ( d.documentElement.clientHeight - rect.bottom ) + rect.height >= 0
  22. );
  23. }, target = d.querySelector("div.pagination"),
  24. page = function(doc, url) {
  25. var images, pageNo = d.createElement("span"), frag = d.createDocumentFragment();
  26. images = [].slice.call(doc.querySelectorAll("span.thumb[id^='s']"));
  27. if (images.length == 0) {
  28. let x = new XMLHttpRequest();
  29. x.open("get", "/intermission.php", false);
  30. x.onreadystatechange = function() {
  31. if (x.readyState == 4)
  32. if (x.status == 200) {
  33. return req(url);
  34. }
  35. };
  36. return x.send();
  37. }
  38. pageNo.textContent = "Page " + url.index + "~";
  39. pageNo.className = "thumb";
  40. frag.appendChild(pageNo);
  41. images.map(a => frag.appendChild(a));
  42. d.querySelector("span.thumb[id^='s']").parentNode.appendChild(frag);
  43. if (list.length > 0) events();
  44. }, req = function(url) {
  45. var x = new XMLHttpRequest();
  46. x.open("get", url.href, false);
  47. x.onreadystatechange = function() {
  48. if (x.readyState == 4)
  49. if (x.status == 200 || x.status == 304) {
  50. let div = d.createElement("div");
  51. div.innerHTML = x.responseText;
  52. page(div, url);
  53. } else {
  54. setTimeout(a => req(url), 5000);
  55. }
  56. };
  57. x.send();
  58. }, process = function() {
  59. if (vis(target)) {
  60. events();
  61. req(list.shift());
  62. }
  63. }, events = function() {
  64. var name = (_on = !_on) ? "addEventListener" : "removeEventListener";
  65. window[name]("scroll", process, false);
  66. window[name]("resize", process, false);
  67. window[name]("visibilitychange", process, false);
  68. }, list = [];
  69. if (!target || !target.querySelector("a")) return;
  70. {
  71. let pagination = target.lastElementChild,
  72. pid = window.location.href.match(/pid=([0-9]+)/),
  73. start_index = pid ? pid[1] / 42 + 1 : 1,
  74. end_index;
  75. if (!pagination.href) return;
  76. else {
  77. end_index = pagination.href.match(/pid=([0-9]+)/)[1] / 42 + 1;
  78. do {
  79. list.push({
  80. href: pagination.href.replace(/pid=[0-9]+/, "pid=" + start_index * 42),
  81. index: ++start_index
  82. });
  83. } while(start_index != end_index);
  84. }
  85. }
  86. events();
  87. process();
  88. }())