SweClockers JavaScript sidladdare

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

当前为 2018-07-22 提交的版本,查看 最新版本

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