Skip to content

Commit

Permalink
perf: ensure settings are the right type
Browse files Browse the repository at this point in the history
  • Loading branch information
theusaf committed Sep 5, 2023
1 parent 5cdc280 commit 932c167
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Sometimes, just an antibot is not enough... Here are ways to prevent bots from j

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

## Support for other play.kahoot.it scripts

Expand Down
59 changes: 52 additions & 7 deletions dist/kahoot-antibot.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @name:ja Kーアンチボット
// @namespace http://tampermonkey.net/
// @homepage https://theusaf.org
// @version 4.2.1
// @version 4.2.2
// @icon https://cdn.discordapp.com/icons/641133408205930506/31c023710d468520708d6defb32a89bc.png
// @description Remove all bots from a kahoot game.
// @description:es eliminar todos los bots de un Kahoot! juego.
Expand Down Expand Up @@ -40,6 +40,25 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
* for helping with contribution and testing of this project
*/
const KANTIBOT_VERSION = GM_info.script.version, kantibotData = {
settingsTypes: {
timeout: "boolean",
looksRandom: "boolean",
blockformat1: "boolean",
blockservice1: "boolean",
blocknum: "boolean",
forceascii: "boolean",
patterns: "boolean",
teamtimeout: "number",
twoFactorTime: "number",
percent: "number",
wordblock: "string",
ddos: "number",
start_lock: "number",
counters: "boolean",
counterCheats: "boolean",
enableCAPTCHA: "boolean",
reduceFalsePositives: "boolean",
},
settings: {
timeout: false,
looksRandom: true,
Expand Down Expand Up @@ -510,10 +529,8 @@ const METHODS = {
return kantibotData.settings[id];
},
setSetting(id, value) {
const localConfig = JSON.parse(window.localStorage.antibotConfig ?? "{}");
localConfig[id] = value;
window.localStorage.antibotConfig = JSON.stringify(localConfig);
kantibotData.settings[id] = value; // as any to avoid weird typescript issue
window.localStorage.kantibotConfig = JSON.stringify(kantibotData.settings);
},
applyCaptchaQuestion(question) {
const answers = ["red", "blue", "yellow", "green"], images = [
Expand Down Expand Up @@ -1138,8 +1155,22 @@ const localConfig = JSON.parse(window.localStorage.kantibotConfig ??
"{}");
for (const setting in localConfig) {
try {
const settingType = kantibotData.settingsTypes[setting];
const current = METHODS.getSetting(setting);
METHODS.setSetting(setting, localConfig[setting] ?? current);
let value = localConfig[setting] ?? current;
if (value)
switch (settingType) {
case "boolean":
value = !!value;
break;
case "number":
value = +value;
break;
case "string":
value = `${value}`;
break;
}
METHODS.setSetting(setting, value);
}
catch {
/* ignored */
Expand Down Expand Up @@ -1480,11 +1511,25 @@ const KANTIBOT_HOOKS = {
if (!socket.webSocket.oldSend) {
socket.webSocket.oldSend = socket.webSocket.send;
socket.webSocket.send = function (data) {
websocketMessageSendHandler(socket, data);
try {
websocketMessageSendHandler(socket, data);
}
catch (err) {
log("Error in websocketMessageSendHandler");
console.error(err);
}
socket.webSocket.oldSend(data);
};
}
if (websocketMessageReceiveVerification(socket, message) === !BOT_DETECTED) {
let verificationResult = !BOT_DETECTED;
try {
verificationResult = websocketMessageReceiveVerification(socket, message);
}
catch (err) {
log("Error in websocketMessageReceiveVerification");
console.error(err);
}
if (verificationResult === !BOT_DETECTED) {
return value.call(this, socket, message);
}
};
Expand Down
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ declare global {
}

interface KAntibotData {
settingsTypes: Record<keyof KAntibotSettings, string>;
settings: KAntibotSettings;
methods: Record<string, CallableFunction>;
runtimeData: {
Expand Down
66 changes: 54 additions & 12 deletions src/kahoot-antibot.user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @name:ja Kーアンチボット
// @namespace http://tampermonkey.net/
// @homepage https://theusaf.org
// @version 4.2.1
// @version 4.2.2
// @icon https://cdn.discordapp.com/icons/641133408205930506/31c023710d468520708d6defb32a89bc.png
// @description Remove all bots from a kahoot game.
// @description:es eliminar todos los bots de un Kahoot! juego.
Expand Down Expand Up @@ -45,6 +45,25 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

const KANTIBOT_VERSION = GM_info.script.version,
kantibotData: KAntibotData = {
settingsTypes: {
timeout: "boolean",
looksRandom: "boolean",
blockformat1: "boolean",
blockservice1: "boolean",
blocknum: "boolean",
forceascii: "boolean",
patterns: "boolean",
teamtimeout: "number",
twoFactorTime: "number",
percent: "number",
wordblock: "string",
ddos: "number",
start_lock: "number",
counters: "boolean",
counterCheats: "boolean",
enableCAPTCHA: "boolean",
reduceFalsePositives: "boolean",
},
settings: {
timeout: false,
looksRandom: true,
Expand Down Expand Up @@ -550,10 +569,8 @@ const METHODS = {
id: keyof KAntibotSettings,
value: T
) {
const localConfig = JSON.parse(window.localStorage.antibotConfig ?? "{}");
localConfig[id] = value;
window.localStorage.antibotConfig = JSON.stringify(localConfig);
(kantibotData.settings as unknown as Record<string, unknown>)[id] = value; // as any to avoid weird typescript issue
window.localStorage.kantibotConfig = JSON.stringify(kantibotData.settings);
},

applyCaptchaQuestion(question: KQuestion): void {
Expand Down Expand Up @@ -1343,11 +1360,23 @@ const localConfig: Record<keyof KAntibotSettings, string | boolean | number> =
);
for (const setting in localConfig) {
try {
const settingType =
kantibotData.settingsTypes[setting as keyof KAntibotSettings];
const current = METHODS.getSetting(setting as keyof KAntibotSettings);
METHODS.setSetting(
setting as keyof KAntibotSettings,
localConfig[setting as keyof KAntibotSettings] ?? current
);
let value = localConfig[setting as keyof KAntibotSettings] ?? current;
if (value)
switch (settingType) {
case "boolean":
value = !!value;
break;
case "number":
value = +value;
break;
case "string":
value = `${value}`;
break;
}
METHODS.setSetting(setting as keyof KAntibotSettings, value);
} catch {
/* ignored */
}
Expand Down Expand Up @@ -1788,13 +1817,26 @@ const KANTIBOT_HOOKS: Record<string, KAntibotHook> = {
if (!socket.webSocket.oldSend) {
socket.webSocket.oldSend = socket.webSocket.send;
socket.webSocket.send = function (data: string) {
websocketMessageSendHandler(socket, data);
try {
websocketMessageSendHandler(socket, data);
} catch (err) {
log("Error in websocketMessageSendHandler");
console.error(err);
}
socket.webSocket.oldSend!(data);
};
}
if (
websocketMessageReceiveVerification(socket, message) === !BOT_DETECTED
) {
let verificationResult = !BOT_DETECTED;
try {
verificationResult = websocketMessageReceiveVerification(
socket,
message
);
} catch (err) {
log("Error in websocketMessageReceiveVerification");
console.error(err);
}
if (verificationResult === !BOT_DETECTED) {
return value.call(this, socket, message);
}
};
Expand Down

0 comments on commit 932c167

Please sign in to comment.