This commit is contained in:
yoannchb-pro
2023-03-22 22:25:17 -04:00
committed by GitHub
parent 02ac6bc494
commit 2663989b4e
9 changed files with 115 additions and 7 deletions
+2 -1
View File
@@ -41,7 +41,7 @@ function setUpMoodleGpt(config: Config) {
}
//injection
const inputQuery = ["checkbox", "radio", "text"]
const inputQuery = ["checkbox", "radio", "text", "number"]
.map((e) => `input[type="${e}"]`)
.join(",");
const query = inputQuery + ", textarea, select";
@@ -49,6 +49,7 @@ function setUpMoodleGpt(config: Config) {
for (const form of forms) {
const hiddenButton: HTMLElement = form.querySelector(".qtext");
if (config.cursor) hiddenButton.style.cursor = "pointer";
const fn = reply.bind(null, config, hiddenButton, form, query);
listeners.push({ element: hiddenButton, fn });
+39
View File
@@ -0,0 +1,39 @@
import Config from "../../types/config";
/**
* Handle number input
* @param config
* @param inputList
* @param response
* @returns
*/
function handleNumber(
config: Config,
inputList: NodeListOf<HTMLElement>,
response: string
): boolean {
const input = inputList[0] as HTMLInputElement | HTMLTextAreaElement;
if (inputList.length !== 1 || input.type !== "number") return false;
const number = response.match(/\d+([,\.]\d+)?/gi)?.[0]?.replace(",", ".");
if (!number) return false;
if (config.typing) {
let index = 0;
input.addEventListener("keydown", function (event: KeyboardEvent) {
if (event.key === "Backspace") index = number.length + 1;
if (index > number.length) return;
event.preventDefault();
if (number.slice(index, index + 1) === ".") ++index;
input.value = number.slice(0, ++index);
});
} else {
input.value = number;
}
return true;
}
export default handleNumber;
+7 -1
View File
@@ -6,6 +6,7 @@ import handleRadioAndCheckbox from "./questions/radio-checkbox";
import handleSelect from "./questions/select";
import handleTextbox from "./questions/textbox";
import handleClipboard from "./questions/clipboard";
import handleNumber from "./questions/number";
/**
* Reply to the question
@@ -38,7 +39,12 @@ async function reply(
if (config.cursor)
hiddenButton.style.cursor = config.infinite ? "pointer" : "initial";
const handlers = [handleTextbox, handleSelect, handleRadioAndCheckbox];
const handlers = [
handleTextbox,
handleNumber,
handleSelect,
handleRadioAndCheckbox,
];
for (const handler of handlers) {
if (handler(config, inputList, response)) return;