Skip to content

Commit 932c167

Browse files
committed
perf: ensure settings are the right type
1 parent 5cdc280 commit 932c167

File tree

4 files changed

+108
-19
lines changed

4 files changed

+108
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Sometimes, just an antibot is not enough... Here are ways to prevent bots from j
7070

7171
- More Blocking Options (When new botting patterns are found).
7272
- Prevent answers during team talk
73+
- Ensuring banned bots are removed from the UI
7374

7475
## Support for other play.kahoot.it scripts
7576

dist/kahoot-antibot.user.js

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @name:ja Kーアンチボット
44
// @namespace http://tampermonkey.net/
55
// @homepage https://theusaf.org
6-
// @version 4.2.1
6+
// @version 4.2.2
77
// @icon https://cdn.discordapp.com/icons/641133408205930506/31c023710d468520708d6defb32a89bc.png
88
// @description Remove all bots from a kahoot game.
99
// @description:es eliminar todos los bots de un Kahoot! juego.
@@ -40,6 +40,25 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
4040
* for helping with contribution and testing of this project
4141
*/
4242
const KANTIBOT_VERSION = GM_info.script.version, kantibotData = {
43+
settingsTypes: {
44+
timeout: "boolean",
45+
looksRandom: "boolean",
46+
blockformat1: "boolean",
47+
blockservice1: "boolean",
48+
blocknum: "boolean",
49+
forceascii: "boolean",
50+
patterns: "boolean",
51+
teamtimeout: "number",
52+
twoFactorTime: "number",
53+
percent: "number",
54+
wordblock: "string",
55+
ddos: "number",
56+
start_lock: "number",
57+
counters: "boolean",
58+
counterCheats: "boolean",
59+
enableCAPTCHA: "boolean",
60+
reduceFalsePositives: "boolean",
61+
},
4362
settings: {
4463
timeout: false,
4564
looksRandom: true,
@@ -510,10 +529,8 @@ const METHODS = {
510529
return kantibotData.settings[id];
511530
},
512531
setSetting(id, value) {
513-
const localConfig = JSON.parse(window.localStorage.antibotConfig ?? "{}");
514-
localConfig[id] = value;
515-
window.localStorage.antibotConfig = JSON.stringify(localConfig);
516532
kantibotData.settings[id] = value; // as any to avoid weird typescript issue
533+
window.localStorage.kantibotConfig = JSON.stringify(kantibotData.settings);
517534
},
518535
applyCaptchaQuestion(question) {
519536
const answers = ["red", "blue", "yellow", "green"], images = [
@@ -1138,8 +1155,22 @@ const localConfig = JSON.parse(window.localStorage.kantibotConfig ??
11381155
"{}");
11391156
for (const setting in localConfig) {
11401157
try {
1158+
const settingType = kantibotData.settingsTypes[setting];
11411159
const current = METHODS.getSetting(setting);
1142-
METHODS.setSetting(setting, localConfig[setting] ?? current);
1160+
let value = localConfig[setting] ?? current;
1161+
if (value)
1162+
switch (settingType) {
1163+
case "boolean":
1164+
value = !!value;
1165+
break;
1166+
case "number":
1167+
value = +value;
1168+
break;
1169+
case "string":
1170+
value = `${value}`;
1171+
break;
1172+
}
1173+
METHODS.setSetting(setting, value);
11431174
}
11441175
catch {
11451176
/* ignored */
@@ -1480,11 +1511,25 @@ const KANTIBOT_HOOKS = {
14801511
if (!socket.webSocket.oldSend) {
14811512
socket.webSocket.oldSend = socket.webSocket.send;
14821513
socket.webSocket.send = function (data) {
1483-
websocketMessageSendHandler(socket, data);
1514+
try {
1515+
websocketMessageSendHandler(socket, data);
1516+
}
1517+
catch (err) {
1518+
log("Error in websocketMessageSendHandler");
1519+
console.error(err);
1520+
}
14841521
socket.webSocket.oldSend(data);
14851522
};
14861523
}
1487-
if (websocketMessageReceiveVerification(socket, message) === !BOT_DETECTED) {
1524+
let verificationResult = !BOT_DETECTED;
1525+
try {
1526+
verificationResult = websocketMessageReceiveVerification(socket, message);
1527+
}
1528+
catch (err) {
1529+
log("Error in websocketMessageReceiveVerification");
1530+
console.error(err);
1531+
}
1532+
if (verificationResult === !BOT_DETECTED) {
14881533
return value.call(this, socket, message);
14891534
}
14901535
};

src/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ declare global {
146146
}
147147

148148
interface KAntibotData {
149+
settingsTypes: Record<keyof KAntibotSettings, string>;
149150
settings: KAntibotSettings;
150151
methods: Record<string, CallableFunction>;
151152
runtimeData: {

src/kahoot-antibot.user.ts

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @name:ja Kーアンチボット
44
// @namespace http://tampermonkey.net/
55
// @homepage https://theusaf.org
6-
// @version 4.2.1
6+
// @version 4.2.2
77
// @icon https://cdn.discordapp.com/icons/641133408205930506/31c023710d468520708d6defb32a89bc.png
88
// @description Remove all bots from a kahoot game.
99
// @description:es eliminar todos los bots de un Kahoot! juego.
@@ -45,6 +45,25 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
4545

4646
const KANTIBOT_VERSION = GM_info.script.version,
4747
kantibotData: KAntibotData = {
48+
settingsTypes: {
49+
timeout: "boolean",
50+
looksRandom: "boolean",
51+
blockformat1: "boolean",
52+
blockservice1: "boolean",
53+
blocknum: "boolean",
54+
forceascii: "boolean",
55+
patterns: "boolean",
56+
teamtimeout: "number",
57+
twoFactorTime: "number",
58+
percent: "number",
59+
wordblock: "string",
60+
ddos: "number",
61+
start_lock: "number",
62+
counters: "boolean",
63+
counterCheats: "boolean",
64+
enableCAPTCHA: "boolean",
65+
reduceFalsePositives: "boolean",
66+
},
4867
settings: {
4968
timeout: false,
5069
looksRandom: true,
@@ -550,10 +569,8 @@ const METHODS = {
550569
id: keyof KAntibotSettings,
551570
value: T
552571
) {
553-
const localConfig = JSON.parse(window.localStorage.antibotConfig ?? "{}");
554-
localConfig[id] = value;
555-
window.localStorage.antibotConfig = JSON.stringify(localConfig);
556572
(kantibotData.settings as unknown as Record<string, unknown>)[id] = value; // as any to avoid weird typescript issue
573+
window.localStorage.kantibotConfig = JSON.stringify(kantibotData.settings);
557574
},
558575

559576
applyCaptchaQuestion(question: KQuestion): void {
@@ -1343,11 +1360,23 @@ const localConfig: Record<keyof KAntibotSettings, string | boolean | number> =
13431360
);
13441361
for (const setting in localConfig) {
13451362
try {
1363+
const settingType =
1364+
kantibotData.settingsTypes[setting as keyof KAntibotSettings];
13461365
const current = METHODS.getSetting(setting as keyof KAntibotSettings);
1347-
METHODS.setSetting(
1348-
setting as keyof KAntibotSettings,
1349-
localConfig[setting as keyof KAntibotSettings] ?? current
1350-
);
1366+
let value = localConfig[setting as keyof KAntibotSettings] ?? current;
1367+
if (value)
1368+
switch (settingType) {
1369+
case "boolean":
1370+
value = !!value;
1371+
break;
1372+
case "number":
1373+
value = +value;
1374+
break;
1375+
case "string":
1376+
value = `${value}`;
1377+
break;
1378+
}
1379+
METHODS.setSetting(setting as keyof KAntibotSettings, value);
13511380
} catch {
13521381
/* ignored */
13531382
}
@@ -1788,13 +1817,26 @@ const KANTIBOT_HOOKS: Record<string, KAntibotHook> = {
17881817
if (!socket.webSocket.oldSend) {
17891818
socket.webSocket.oldSend = socket.webSocket.send;
17901819
socket.webSocket.send = function (data: string) {
1791-
websocketMessageSendHandler(socket, data);
1820+
try {
1821+
websocketMessageSendHandler(socket, data);
1822+
} catch (err) {
1823+
log("Error in websocketMessageSendHandler");
1824+
console.error(err);
1825+
}
17921826
socket.webSocket.oldSend!(data);
17931827
};
17941828
}
1795-
if (
1796-
websocketMessageReceiveVerification(socket, message) === !BOT_DETECTED
1797-
) {
1829+
let verificationResult = !BOT_DETECTED;
1830+
try {
1831+
verificationResult = websocketMessageReceiveVerification(
1832+
socket,
1833+
message
1834+
);
1835+
} catch (err) {
1836+
log("Error in websocketMessageReceiveVerification");
1837+
console.error(err);
1838+
}
1839+
if (verificationResult === !BOT_DETECTED) {
17981840
return value.call(this, socket, message);
17991841
}
18001842
};

0 commit comments

Comments
 (0)