TEXT   198   0
   418 4.05 KB    183

Flag Randomizer Userscript

By Fillyanon
Created: 2022-01-18 19:30:27
Updated: 2022-01-19 09:19:28
Expiry: Never

  1. // ==UserScript==
  2. // @name Flag Randomizer
  3. // @grant none
  4. // @version 1.18
  5. // @author Fillyanon, ScriptFilly
  6. // @description Adds a flag randomizer to the Board.
  7. // ==/UserScript==
  8.  
  9. 'use strict'
  10.  
  11. let changeFlagOnKeyStroke = true;
  12.  
  13. const flags = Object.freeze({
  14. MISC: ["4CC", "AN"],
  15.  
  16. G4: [
  17. "AJ",
  18. "ANF",
  19. "APB",
  20. "AU",
  21. "BB",
  22. "BM",
  23. "BP",
  24. "BS",
  25. "CB",
  26. "CG",
  27. "CHE",
  28. "CL",
  29. "CO",
  30. "DAY",
  31. "DD",
  32. "DER",
  33. "DIS",
  34. "DT",
  35. "FAU",
  36. "FL",
  37. "FLE",
  38. "GI",
  39. "LI",
  40. "LT",
  41. "LY",
  42. "MA",
  43. "MAU",
  44. "MIN",
  45. "NI",
  46. "NUR",
  47. "OCT",
  48. "PAR",
  49. "PC",
  50. "PCE",
  51. "PI",
  52. "PLU",
  53. "PM",
  54. "QC",
  55. "RAR",
  56. "RD",
  57. "RLU",
  58. "S1L",
  59. "SCO",
  60. "SHI",
  61. "SIL",
  62. "SP",
  63. "SPI",
  64. "STA",
  65. "STL",
  66. "SUN",
  67. "SUS",
  68. "SWB",
  69. "TS",
  70. "TWI",
  71. "TX",
  72. "VS",
  73. "ZE",
  74. ],
  75.  
  76. G5: ["HT", "IZ", "PP", "SPT", "ZS", "SS"],
  77.  
  78. EQG: [
  79. "ADA",
  80. "AB",
  81. "SON",
  82. "EQA",
  83. "EQF",
  84. "EQP",
  85. "EQR",
  86. "EQT",
  87. "EQI",
  88. "EQS",
  89. "ERA",
  90. ],
  91.  
  92. TFH: ["TFA", "TFO", "TFP", "TFS", "TFT", "TFV", "TP"],
  93. });
  94.  
  95. // Collects all flags into a single, flat array.
  96. const allFlags = Object.values(flags).flat();
  97.  
  98. // Returns a random element from an array.
  99. const getRand = (coll) => {
  100. return coll[Math.floor(Math.random() * coll.length)];
  101. };
  102.  
  103. const makeOpt = (option) => {
  104. const opt = document.createElement("option");
  105. opt.value = opt.innerText = option;
  106. selector.appendChild(opt);
  107. }
  108.  
  109. const getPost = (use4chanX) => {
  110. let post = (use4chanX !== "") ? use4chanX : document.forms.post;
  111. return post;
  112. }
  113.  
  114. let handler = false;
  115.  
  116. const changeFlag = () => {
  117. let flagSelector = post.querySelector(".flagSelector");
  118. if (selector.value == "OFF") return;
  119. flagSelector.value = (selector.value != "ALL") ? getRand(flags[selector.value]) : getRand(allFlags);
  120. }
  121.  
  122. const selector = document.createElement("select");
  123. const botLine = document.querySelector('.navLinksBot');
  124.  
  125. selector.style = "margin-left: 1rem;";
  126. selector.addEventListener("change", () => {
  127. window.localStorage.setItem("flagGroup", selector.value);
  128. });
  129.  
  130. Object.keys(flags).forEach((key) => {makeOpt(key);});
  131. makeOpt("ALL");
  132. makeOpt("OFF");
  133. selector.value = window.localStorage.getItem("flagGroup") ?? "G4";
  134.  
  135. botLine.appendChild(selector);
  136.  
  137. function debounce(func, wait, immediate) {
  138. var timeout;
  139. return function() {
  140. var context = this, args = arguments;
  141. var later = function() {
  142. timeout = null;
  143. if (!immediate) func.apply(context, args);
  144. };
  145. var callNow = immediate && !timeout;
  146. clearTimeout(timeout);
  147. timeout = setTimeout(later, wait);
  148. if (callNow) func.apply(context, args);
  149. };
  150. }
  151. var myEfficientFn = debounce(function() { changeFlag(); }, 250, false);
  152.  
  153. let handled = true;
  154. if (changeFlagOnKeyStroke == true) {
  155. const config = {attributes: true, childList: true, subtree: true};
  156. const callback = function (mutations, _) {
  157. for (const mutation of mutations) {
  158. if (mutation.type === 'childList') { // When new child is added
  159. let use4chanX = document.getElementById("qr") ?? document.getElementById("quickReply") ?? "";
  160. post = getPost(use4chanX);
  161.  
  162. // Do nothing if post is not loaded
  163. if (post == null || post == undefined) {return;}
  164.  
  165. // Else update the flag
  166. let reply = (use4chanX !== "") ? use4chanX.childNodes[1][5] : post[8];
  167. reply.addEventListener('input', myEfficientFn);
  168. }
  169. }
  170. };
  171. const observer = new MutationObserver(callback);
  172. observer.observe(document, config);
  173. } else {
  174. setInterval(() => {
  175. let use4chanX = document.getElementById("qr") ?? "";
  176. post = getPost(use4chanX);
  177. if (post == null || post == undefined) { return; }
  178. changeFlag();
  179. }, 500);
  180. }

Fillyanon's Bookshelf

by Fillyanon

Thus passes threadly glory

by Fillyanon

Flag Randomizer Userscript

by Fillyanon

Post Previewer UserScript

by Fillyanon

MLPFicReviews Extras

by Fillyanon