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