Xenforo minimize threads

Adds a button to minimize thread forums

  1. // ==UserScript==
  2. // @name Xenforo minimize threads
  3. // @version 1
  4. // @grant Uvigz
  5. // @description Adds a button to minimize thread forums
  6. // @match *://forums.spacebattles.com/forums/*/page*
  7. // @match *://forums.spacebattles.com/forums/*/?*
  8. // @match *://forums.spacebattles.com/forums/*/
  9. // @match *://forums.sufficientvelocity.com/forums/*/page*
  10. // @match *://forums.sufficientvelocity.com/forums/*/?*
  11. // @match *://forums.sufficientvelocity.com/forums/*/
  12. // @namespace https://greasyfork.org/users/42312
  13. // ==/UserScript==
  14.  
  15. // if container's elements should be hidden.
  16. var hidden = function(el, className) {
  17. console.log('hidden');
  18. // prepare button
  19. var button;
  20. // then for every children
  21. [...el.children].forEach(element => {
  22. // check if its title
  23. if (element.classList.contains('structItem-cell--main')) {
  24. // if yes should leave title alone and deal with button, check all of its children
  25. [...element.children].forEach(element2 => {
  26. // check if its hide button
  27. if (element2.classList.contains('hide-container')) {
  28. // if yes, save its reference.
  29. button = element2;
  30. }
  31. // check if its title
  32. else if (element2.classList.contains('structItem-title')){
  33. // if its title, then add code for better design
  34. element2.style.display = 'inline-block';
  35. } else {
  36. // if no, then hide it
  37. element2.style.display = 'none';
  38. }
  39. });
  40. // then deal with button
  41. console.log(el, element)
  42. dealWithButtom('unhide', unhide, el, element, className, button);
  43. } else {
  44. console.log(element);
  45. // if no, then hide it
  46. element.style.display = 'none';
  47. }
  48. });
  49. }
  50.  
  51.  
  52. // if you want to hide element
  53. var hide = function(el, className) {
  54. // do all the thinks as if element was hidden
  55. hidden(el, className);
  56. // then add elements key to hidden elements list
  57. hiddenList.push(className);
  58. // and then store the list in local storage
  59. localStorage.setItem(hostname + "hiddenList", JSON.stringify(hiddenList));
  60. }
  61.  
  62. // if you want to unhide element
  63. var unhide = function(el, className) {
  64. // do all the thinks as if element was unhidden
  65. unhidden(el, className);
  66. // then remnove elements key from hidden elements array
  67. // (this actually creanes new filtered array without
  68. // elements key, then overwrites the array, but practically,
  69. // it should be the same as deleting elements key from array
  70. // but it's easier to read)
  71. hiddenList = hiddenList.filter(e => e !== className)
  72. // and then store the list in local storage
  73. localStorage.setItem(hostname + "hiddenList", JSON.stringify(hiddenList));
  74. }
  75.  
  76. // if container's elements should be unhidden.
  77. var unhidden = function(el, className) {
  78. console.log('unhidden');
  79. // prepare button
  80. var button;
  81. // then for every children
  82. [...el.children].forEach(element => {
  83. // check if its title
  84. if (element.classList.contains('structItem-cell--main')) {
  85. // if yes should leave title alone and deal with button, check all of its children
  86. [...element.children].forEach(element2 => {
  87. // check if its hide button
  88. if (element2.classList.contains('hide-container')) {
  89. // if yes, save its reference.
  90. button = element2;
  91. }
  92. // check if its title
  93. else if (element2.classList.contains('structItem-title')){
  94. // if its title, then add code for better design
  95. element2.style.display = 'initial';
  96. } else {
  97. // if no, then hide it
  98. element2.style.display = 'initial';
  99. }
  100. });
  101. // then deal with button
  102. console.log(el, element)
  103. dealWithButtom('hide', hide, el, element, className, button);
  104. } else {
  105. console.log(element);
  106. // if no, then hide it
  107. element.style.display = 'table-cell';
  108. }
  109. });
  110. }
  111.  
  112. dealWithButtom = function(text, functionReference, el, container, className, button) {
  113. if (!button) {
  114. button = document.createElement ('div');
  115. button.className = 'hide-container button--cta button';
  116. button.innerHTML = text;
  117. button.onclick = () => {functionReference(el, className)};
  118. // and add it to container
  119. container.appendChild(button);
  120. } else {
  121. // if there was, edit it in place
  122. button.innerHTML =text;
  123. button.onclick = () => {functionReference(el, className)};
  124. }
  125. }
  126.  
  127. // on page load: get hidden elements list from local storage,
  128. // or if there's nothing in localstorage, create new array
  129. var hostname = window.location.hostname;
  130. var hiddenList = JSON.parse(localStorage.getItem(hostname + 'hiddenList')) || [];
  131. // select all threads
  132. document.querySelectorAll(".structItem--thread.js-inlineModContainer").forEach(el =>{
  133. // get threads class key
  134. var className = el.className.match(/js-threadListItem-\d+/);
  135. if(className){
  136. className = className[0];
  137. }
  138.  
  139. // then check if it should be hidden
  140. if (hiddenList.filter(e => e === className).length > 0){
  141. // if yes, hide it
  142. hidden(el, className);
  143. } else {
  144. // if no, then create hide button
  145. unhidden(el, className);
  146. };
  147. });
  148.  
  149. var style = document.createElement('style');
  150. style.innerHTML = `
  151. .hide-container {
  152. float: right;
  153. }
  154. `;
  155. document.head.appendChild(style);