SweClockers JavaScript sidladdare

Lägger till en knapp som läser in kommande inlägg utan uppdatering av sidan.

  1. // ==UserScript==
  2. // @name SweClockers JavaScript sidladdare
  3. // @namespace hAPsdWxok4bTePK8JZdG
  4. // @author LemonIllusion
  5. // @version 2.0.2
  6. // @match https://www.sweclockers.com/forum/trad/*
  7. // @match https://www.sweclockers.com/forum/post/*
  8. // @description Lägger till en knapp som läser in kommande inlägg utan uppdatering av sidan.
  9. // ==/UserScript==
  10.  
  11. function getDocument(url, callback) {
  12. let xhr = new XMLHttpRequest();
  13. xhr.addEventListener("load", function() {
  14. callback(xhr.responseXML)
  15. });
  16. xhr.open("GET", url);
  17. xhr.responseType = "document";
  18. xhr.send();
  19. }
  20.  
  21. let nextURL, canLoad = false;
  22.  
  23. HTMLDocument.prototype.hasNextPage = function() {
  24. return this.getElementsByClassName("next-page").length !== 0
  25. };
  26.  
  27. HTMLDocument.prototype.getNextPageURL = function() {
  28. return this.getElementsByClassName("next-page")[0].href
  29. };
  30.  
  31. HTMLDocument.prototype.prepareNextPage = function() {
  32. if (this.hasNextPage()) {
  33. nextURL = this.getNextPageURL();
  34. nextButton.setText("Ladda fler inlägg");
  35. nextButton.classList.add("clickable");
  36. canLoad = true;
  37. } else {
  38. nextButton.setText("Det finns inga fler inlägg att ladda");
  39. }
  40. }
  41.  
  42. HTMLDocument.prototype.importPosts = function() {
  43. let forumPosts = this.getElementsByClassName("forumPosts")[0];
  44. nextButton.parentNode.insertBefore(forumPosts, nextButton);
  45. for (let script of forumPosts.getElementsByTagName("script")) {
  46. new Function(script.text)();
  47. }
  48. }
  49.  
  50. let defaultNextButtons = document.getElementsByClassName("next-page");
  51. let isCurrentPage = Array.from(document.getElementsByClassName("isCurrent")); // för HTMLCollections är dynamiska och loopar idiotiskt med for of
  52.  
  53. function loadNext() {
  54. if (canLoad) {
  55. canLoad = false;
  56. this.classList.remove("clickable");
  57. this.setText("Laddar...");
  58. getDocument(nextURL, function(doc) {
  59. doc.importPosts();
  60. doc.prepareNextPage();
  61. for (let button of defaultNextButtons) {
  62. button.href = nextURL;
  63. }
  64. for (let page of isCurrentPage) {
  65. page.classList.remove("isCurrent");
  66. isCurrentPage = [];
  67. }
  68. });
  69. }
  70. }
  71.  
  72. {
  73. let style = document.createElement('style');
  74. document.head.appendChild(style);
  75. style.sheet.insertRule(".forumPosts {margin-bottom: 8px;}");
  76. style.sheet.insertRule(".nextButton {margin-bottom: 16px; text-align: center;}");
  77. style.sheet.insertRule(".nextButton.clickable {cursor: pointer}");
  78. }
  79.  
  80. let nextButton = document.createElement("div");
  81. nextButton.className = "forumPost nextButton";
  82. nextButton.innerHTML = '<div class="postHeader table"><div class="row"><div class="cell">Förbereder...</div></div></div>';
  83. nextButton.setText = function(text) {
  84. this.getElementsByClassName("cell")[0].innerHTML = text
  85. };
  86. nextButton.addEventListener("click", loadNext);
  87. {
  88. let forumControls = document.getElementsByClassName("forumControls")[1];
  89. forumControls.parentNode.insertBefore(nextButton, forumControls);
  90. }
  91.  
  92. document.prepareNextPage();