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

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