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