This commit is contained in:
yoannchb-pro
2023-06-19 22:08:27 -04:00
parent 88088c0acf
commit 47b239ae54
23 changed files with 432 additions and 432 deletions
+2 -2
View File
@@ -29,7 +29,7 @@ function codeListener(config: Config) {
* @returns
*/
function setUpMoodleGpt(config: Config) {
//removing events
/* Removing events */
if (listeners.length > 0) {
for (const listener of listeners) {
if (config.cursor) listener.element.style.cursor = "initial";
@@ -40,7 +40,7 @@ function setUpMoodleGpt(config: Config) {
return;
}
//injection
/* Code injection */
const inputQuery = ["checkbox", "radio", "text", "number"]
.map((e) => `input[type="${e}"]`)
.join(",");
@@ -1,32 +1,30 @@
import Config from "../types/config";
import normalizeText from "../utils/normalize-text";
import htmlTableToString from "../utils/html-table-to-string";
/**
* Normalize the question and add sub informations
* @param langage
* @param question
* @returns
*/
function normalizeQuestion(config: Config, questionContainer: HTMLElement) {
let question = questionContainer.textContent;
if (config.table) {
//make table more readable for chat-gpt
const tables: NodeListOf<HTMLTableElement> =
questionContainer.querySelectorAll(".qtext table");
for (const table of tables) {
question = question.replace(
table.textContent,
"\n" + htmlTableToString(table) + "\n"
);
}
}
const finalQuestion = `Give a short response as possible for this question, reply in the following question langage and only show the result:
${question}
(If you have to choose between multiple results only show the corrects one, separate them with new line and take the same text as the question)`;
return normalizeText(finalQuestion);
}
export default normalizeQuestion;
import Config from "../types/config";
import normalizeText from "../utils/normalize-text";
import htmlTableToString from "../utils/html-table-to-string";
/**
* Normalize the question and add sub informations
* @param langage
* @param question
* @returns
*/
function createQuestion(config: Config, questionContainer: HTMLElement) {
let question = questionContainer.innerText;
/* Make tables more readable for chat-gpt */
const tables: NodeListOf<HTMLTableElement> =
questionContainer.querySelectorAll(".qtext table");
for (const table of tables) {
question = question.replace(
table.innerText,
"\n" + htmlTableToString(table) + "\n"
);
}
const finalQuestion = `Give a short response as possible for this question, reply in the following question langage and only show the result:
${question}
(If you have to choose between multiple results only show the corrects one, separate them with new line and take the same text as the question)`;
return normalizeText(finalQuestion);
}
export default createQuestion;
+1 -1
View File
@@ -21,7 +21,7 @@ function handleContentEditable(
event.preventDefault();
input.textContent = response.slice(0, ++index);
//put the cursor at the end
/* Put the cursor at the end of the typed text */
input.focus();
const range = document.createRange();
range.selectNodeContents(input);
+2 -2
View File
@@ -29,7 +29,7 @@ function handleSelect(
const content = normalizeText(option.textContent);
const valide = correct[j].includes(content);
//if it's a put in order
/* Handle put in order question */
if (!isNaN(parseInt(content))) {
const content = normalizeText(
(option.parentNode as HTMLElement)
@@ -56,7 +56,7 @@ function handleSelect(
break;
}
}
//end put in order
/* End */
if (config.logs) Logs.responseTry(content, valide);
+2 -4
View File
@@ -1,7 +1,7 @@
import Config from "../types/config";
import Logs from "../utils/logs";
import getChatGPTResponse from "./get-response";
import normalizeQuestion from "./normalize-question";
import createQuestion from "./create-question";
import handleRadioAndCheckbox from "./questions/radio-checkbox";
import handleSelect from "./questions/select";
import handleTextbox from "./questions/textbox";
@@ -25,9 +25,7 @@ async function reply(
) {
if (config.cursor) hiddenButton.style.cursor = "wait";
form.querySelector(".accesshide")?.remove();
const question = normalizeQuestion(config, form);
const question = createQuestion(config, form);
const inputList: NodeListOf<HTMLElement> = form.querySelectorAll(query);
const response = await getChatGPTResponse(config, question).catch(
-1
View File
@@ -8,7 +8,6 @@ type Config = {
cursor?: boolean;
logs?: boolean;
title?: boolean;
table?: boolean;
timeout?: boolean;
mode?: "autocomplete" | "question-to-answer" | "clipboard";
};
+5 -2
View File
@@ -26,8 +26,11 @@ function htmlTableToString(table: HTMLTableElement) {
"\n" + Array(lineSeparationSize).fill("-").join("") + "\n";
const mappedTab = tab.map((line) => {
const mappedLine = line.map(
(content, index) => content.padEnd(maxColumnsLength[index], "\u00A0") //for no matching with \s
const mappedLine = line.map((content, index) =>
content.padEnd(
maxColumnsLength[index],
"\u00A0" /* For no matching with \s */
)
);
return "| " + mappedLine.join(" | ") + " |";
});
+2 -1
View File
@@ -4,7 +4,8 @@
*/
function normalizeText(text: string) {
return text
.replace(/(\n\s*)+/gi, "\n")
.replace(/\n+/gi, "\n")
.replace(/(\n\s*\n)+/g, "\n") //remove useless white sapce from textcontent
.replace(/[ \t]+/gi, " ")
.toLowerCase()
.trim()