diff --git a/extension/moodle-gpt.js b/extension/moodle-gpt.js index 9eea8d3..cec8c22 100644 --- a/extension/moodle-gpt.js +++ b/extension/moodle-gpt.js @@ -1,266 +1,345 @@ -chrome.storage.sync.get(["moodleGPT"]).then(function (storage) { - const config = storage.moodleGPT; +(function (factory) { + typeof define === 'function' && define.amd ? define(factory) : + factory(); +})((function () { 'use strict'; + + /** + * Show some informations into the document title and remove it after 3000ms + * @param text + */ + function titleIndications(text) { + const backTitle = document.title; + document.title = text; + setTimeout(() => (document.title = backTitle), 3000); + } + + /****************************************************************************** + Copyright (c) Microsoft Corporation. - if (!config) throw new Error("Please configure MoodleGPT into the extension"); + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted. - //listening to the keys to inject moodleGPT - const pressedKeys = []; - const listeners = []; - document.body.addEventListener("keydown", function (event) { - pressedKeys.push(event.key); - if (pressedKeys.length > config.code.length) pressedKeys.shift(); - if (pressedKeys.join("") === config.code) { - pressedKeys.length = 0; - setUpMoodleGpt(); - } - }); + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + PERFORMANCE OF THIS SOFTWARE. + ***************************************************************************** */ - /** - * Show some informations into the document title and remove it after 3000ms - * @param {*} text - */ - function titleIndications(text) { - const backTitle = document.title; - document.title = text; - setTimeout(() => (document.title = backTitle), 3000); - } - - /** - * Setup moodleGPT into the page (remove/injection) - */ - function setUpMoodleGpt() { - //removing events - if (listeners.length > 0) { - for (const listener of listeners) { - if (config.cursor) listener.element.style.cursor = "initial"; - listener.element.removeEventListener("click", listener.fn, { - once: !config.infinite, - }); - } - if (config.title) titleIndications("Removed"); - listeners.length = 0; - return; - } - - //injection - const inputQuery = ["checkbox", "radio", "text"] - .map((e) => `input[type="${e}"]`) - .join(","); - const query = inputQuery + ", textarea, select"; - const forms = Array.from(document.querySelectorAll(".formulation")); - - for (const form of forms) { - const hiddenButton = form.querySelector(".qtext"); - if (config.cursor) hiddenButton.style.cursor = "pointer"; - const fn = reply.bind(null, hiddenButton, form, query); - listeners.push({ element: hiddenButton, fn }); - hiddenButton.addEventListener("click", fn, { once: !config.infinite }); - } - - if (config.title) titleIndications("Injected"); - } - - /** - * Normlize text - * @param {*} text - */ - function normalizeText(text) { - return text - .replace(/\n+/g, "\n") - .replace(/[ \t]+/g, " ") - .toLowerCase() - .trim() - .replace(/^[a-z\d]\.\s/gi, "") //a. text, b. text, c. text, 1. text, 2. text, 3.text - .replace(/\n[a-z\d]\.\s/gi, "\n"); //a. text, b. text, c. text, 1. text, 2. text, 3.text - } - - /** - * Get the response from chatGPT api - * @param {*} question - * @returns - */ - async function getChatGPTResponse(question) { - const req = await fetch("https://api.openai.com/v1/chat/completions", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${config.apiKey}`, - }, - body: JSON.stringify({ - model: - config.model && config.model !== "" ? config.model : "gpt-3.5-turbo", - messages: [{ role: "user", content: question }], - temperature: 0.8, - top_p: 1.0, - presence_penalty: 1.0, - stop: null, - }), - }); - const rep = await req.json(); - const response = rep.choices[0].message.content; - return normalizeText(response); - } - - /** - * Handling logs into the console - */ - class Logs { - static question(text) { - const css = "color: cyan"; - console.log("%c[QUESTION]: %s", css, text); - } - - static responseTry(text, valide) { - const css = "color: " + (valide ? "green" : "red"); - console.log("%c[CHECKING]: %s", css, text); - } - - static array(arr) { - console.log("[CORRECTS] ", arr); - } - - static response(text) { - console.log(text); - } - } - - /** - * Reply to the question - * @param {*} form - * @param {*} query - * @returns - */ - async function reply(hiddenButton, form, query) { - if (config.cursor) hiddenButton.style.cursor = "wait"; - - form.querySelector(".accesshide")?.remove(); - - const question = normalizeText(form.textContent); - const inputList = form.querySelectorAll(query); - - const finalQuestion = `Give a short response as possible for this question, reply in ${ - config.langage && config.langage !== "" - ? 'this langage "' + config.langage + '"' - : "the following question langage" - } and only show the result: + function __awaiter(thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); + } + + class Logs { + static question(text) { + const css = "color: cyan"; + console.log("%c[QUESTION]: %s", css, text); + } + static responseTry(text, valide) { + const css = "color: " + (valide ? "green" : "red"); + console.log("%c[CHECKING]: %s", css, text); + } + static array(arr) { + console.log("[CORRECTS] ", arr); + } + static response(text) { + console.log(text); + } + } + + /** + * Normlize text + * @param text + */ + function normalizeText(text) { + return text + .replace(/\n+/g, "\n") + .replace(/[ \t]+/g, " ") + .toLowerCase() + .trim() + .replace(/^[a-z\d]\.\s/gi, "") //a. text, b. text, c. text, 1. text, 2. text, 3.text + .replace(/\n[a-z\d]\.\s/gi, "\n"); //same but with new line + } + + /** + * Get the response from chatGPT api + * @param config + * @param question + * @returns + */ + function getChatGPTResponse(config, question) { + return __awaiter(this, void 0, void 0, function* () { + const req = yield fetch("https://api.openai.com/v1/chat/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${config.apiKey}`, + }, + body: JSON.stringify({ + model: config.model && config.model !== "" ? config.model : "gpt-3.5-turbo", + messages: [{ role: "user", content: question }], + temperature: 0.8, + top_p: 1.0, + presence_penalty: 1.0, + stop: null, + }), + }); + const rep = yield req.json(); + const response = rep.choices[0].message.content; + return normalizeText(response); + }); + } + + /** + * Normalize the question and add sub informations + * @param langage + * @param question + * @returns + */ + function normalizeQuestion(langage, question) { + const finalQuestion = `Give a short response as possible for this question, reply in ${langage && langage !== "" + ? 'this langage "' + langage + '"' + : "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)`; - - const response = await getChatGPTResponse(finalQuestion); - - if (config.logs) { - Logs.question(finalQuestion); - Logs.response(response); - } - - if (config.cursor) - hiddenButton.style.cursor = config.infinite ? "pointer" : "initial"; - - //if we dont find the input we copy into the clipboard - if (inputList.length === 0) { - if (config.title) titleIndications("Copied to clipboard"); - navigator.clipboard.writeText(response); - return; - } - - //if it's a text - if ( - (inputList.length === 1 && inputList[0].type === "text") || - inputList[0].tagName === "TEXTAREA" - ) { - if (config.typing) { - let index = 0; - inputList[0].addEventListener("keydown", function (event) { - if (event.key === "Backspace") index = response.length + 1; - if (index > response.length) return; - event.preventDefault(); - inputList[0].value = response.slice(0, ++index); - }); - } else { - inputList[0].value = response; - } - return; - } - - //if it's a select - if (inputList[0].tagName === "SELECT") { - let correct = response.split("\n"); - if (correct.length === 1 && correct.length !== inputList) - correct = response.split(","); - - if (config.logs) Logs.array(correct); - - for (let j = 0; j < inputList.length; ++j) { - const options = inputList[j].querySelectorAll("option"); - - for (const option of options) { - const content = normalizeText(option.textContent); - const valide = correct[j].includes(content); - - //if it's a put in order - if (!isNaN(parseInt(content))) { - const content = normalizeText( - option.parentNode.closest("tr").querySelector(".text").textContent - ); - const index = correct.findIndex((c) => { - const valide = c.includes(content); - if (config.logs) Logs.responseTry(content, valide); - return valide; - }); - if (index !== -1) { - if (config.mouseover) { - options[index + 1].closest("select").addEventListener( - "click", - function () { - options[index + 1].selected = "selected"; - }, - { once: true } - ); - } else { - options[index + 1].selected = "selected"; - } - break; - } - } - - if (config.logs) Logs.responseTry(content, valide); - if (valide) { - if (config.mouseover) { - option.closest("select").addEventListener( - "click", - function () { - option.selected = "selected"; - }, - { once: true } - ); - } else { - option.selected = "selected"; - } - break; - } - } - } - return; - } - - //if it's a radio button or checkbox - for (const input of inputList) { - const content = normalizeText(input.parentNode.textContent); - const valide = response.includes(content); - if (config.logs) Logs.responseTry(content, valide); - if (valide) { - if (config.mouseover) { - input.addEventListener( - "mouseover", - function (event) { - event.target.checked = true; - }, - { once: true } - ); - } else { - input.checked = true; - } - } - } - } -}); + (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); + } + + /** + * Handle checkbox and input elements + * @param config + * @param inputList + * @param response + */ + function handleRadioAndCheckbox(config, inputList, response) { + const input = inputList === null || inputList === void 0 ? void 0 : inputList[0]; + if (!input || (input.type !== "checkbox" && input.type !== "radio")) + return false; + for (const input of inputList) { + const content = normalizeText(input.parentNode.textContent); + const valide = response.includes(content); + if (config.logs) + Logs.responseTry(content, valide); + if (valide) { + if (config.mouseover) { + input.addEventListener("mouseover", () => (input.checked = true), { + once: true, + }); + } + else { + input.checked = true; + } + } + } + return true; + } + + /** + * Handle select elements (and put in order select) + * @param config + * @param inputList + * @param response + * @returns + */ + function handleSelect(config, inputList, response) { + if (inputList.length === 0 || inputList[0].tagName !== "SELECT") + return false; + let correct = response.split("\n"); + if (correct.length === 1 && correct.length !== inputList.length) + correct = response.split(","); + if (config.logs) + Logs.array(correct); + for (let j = 0; j < inputList.length; ++j) { + const options = inputList[j].querySelectorAll("option"); + for (const option of options) { + const content = normalizeText(option.textContent); + const valide = correct[j].includes(content); + //if it's a put in order + if (!isNaN(parseInt(content))) { + const content = normalizeText(option.parentNode + .closest("tr") + .querySelector(".text").textContent); + const index = correct.findIndex((c) => { + const valide = c.includes(content); + if (config.logs) + Logs.responseTry(content, valide); + return valide; + }); + if (index !== -1) { + if (config.mouseover) { + options[index + 1].closest("select").addEventListener("click", function () { + options[index + 1].selected = "selected"; + }, { once: true }); + } + else { + options[index + 1].selected = "selected"; + } + break; + } + } + //end put in order + if (config.logs) + Logs.responseTry(content, valide); + if (valide) { + if (config.mouseover) { + option + .closest("select") + .addEventListener("click", () => (option.selected = true), { + once: true, + }); + } + else { + option.selected = true; + } + break; + } + } + } + return true; + } + + /** + * Handle textbox + * @param config + * @param inputList + * @param response + * @returns + */ + function handleTextbox(config, inputList, response) { + const input = inputList[0]; + if (inputList.length !== 1 || + (input.tagName !== "TEXTAREA" && input.type !== "text")) + return false; + if (config.typing) { + let index = 0; + input.addEventListener("keydown", function (event) { + if (event.key === "Backspace") + index = response.length + 1; + if (index > response.length) + return; + event.preventDefault(); + input.value = response.slice(0, ++index); + }); + } + else { + input.value = response; + } + return true; + } + + /** + * Copy the response in the clipboard if we can automaticaly fill the question + * @param config + * @param inputList + * @param response + * @param force Force the copy to clipboard + * @returns + */ + function handleClipboard(config, response) { + if (config.title) + titleIndications("Copied to clipboard"); + navigator.clipboard.writeText(response); + } + + /** + * Reply to the question + * @param config + * @param hiddenButton + * @param form + * @param query + * @returns + */ + function reply(config, hiddenButton, form, query) { + var _a; + return __awaiter(this, void 0, void 0, function* () { + if (config.cursor) + hiddenButton.style.cursor = "wait"; + (_a = form.querySelector(".accesshide")) === null || _a === void 0 ? void 0 : _a.remove(); + const question = normalizeQuestion(config.langage, form.textContent); + const inputList = form.querySelectorAll(query); + const response = yield getChatGPTResponse(config, question); + if (config.logs) { + Logs.question(question); + Logs.response(response); + } + if (config.cursor) + hiddenButton.style.cursor = config.infinite ? "pointer" : "initial"; + const handlers = [handleTextbox, handleSelect, handleRadioAndCheckbox]; + for (const handler of handlers) { + if (handler(config, inputList, response)) + return; + } + handleClipboard(config, response); + }); + } + + const pressedKeys = []; + const listeners = []; + /** + * Create a listener on the keyboard to inject the code + * @param config + */ + function codeListener(config) { + document.body.addEventListener("keydown", function (event) { + pressedKeys.push(event.key); + if (pressedKeys.length > config.code.length) + pressedKeys.shift(); + if (pressedKeys.join("") === config.code) { + pressedKeys.length = 0; + setUpMoodleGpt(config); + } + }); + } + /** + * Setup moodleGPT into the page (remove/injection) + * @param config + * @returns + */ + function setUpMoodleGpt(config) { + //removing events + if (listeners.length > 0) { + for (const listener of listeners) { + if (config.cursor) + listener.element.style.cursor = "initial"; + listener.element.removeEventListener("click", listener.fn); + } + if (config.title) + titleIndications("Removed"); + listeners.length = 0; + return; + } + //injection + const inputQuery = ["checkbox", "radio", "text"] + .map((e) => `input[type="${e}"]`) + .join(","); + const query = inputQuery + ", textarea, select"; + const forms = Array.from(document.querySelectorAll(".formulation")); + for (const form of forms) { + const hiddenButton = form.querySelector(".qtext"); + if (config.cursor) + hiddenButton.style.cursor = "pointer"; + const fn = reply.bind(null, config, hiddenButton, form, query); + listeners.push({ element: hiddenButton, fn }); + hiddenButton.addEventListener("click", fn, { once: !config.infinite }); + } + if (config.title) + titleIndications("Injected"); + } + + chrome.storage.sync.get(["moodleGPT"]).then(function (storage) { + const config = storage.moodleGPT; + if (!config) + throw new Error("Please configure MoodleGPT into the extension"); + codeListener(config); + }); + +})); +//# sourceMappingURL=moodle-gpt.js.map diff --git a/extension/moodle-gpt.js.map b/extension/moodle-gpt.js.map new file mode 100644 index 0000000..0c7a1b8 --- /dev/null +++ b/extension/moodle-gpt.js.map @@ -0,0 +1 @@ +{"version":3,"file":"moodle-gpt.js","sources":["../src/utils/title-indications.ts","../node_modules/tslib/tslib.es6.js","../src/utils/logs.ts","../src/utils/normalize-text.ts","../src/core/get-response.ts","../src/core/normalize-question.ts","../src/core/questions/radio-checkbox.ts","../src/core/questions/select.ts","../src/core/questions/textbox.ts","../src/core/questions/clipboard.ts","../src/core/reply.ts","../src/core/code-listener.ts","../src/index.ts"],"sourcesContent":["/**\r\n * Show some informations into the document title and remove it after 3000ms\r\n * @param text\r\n */\r\nfunction titleIndications(text: string) {\r\n const backTitle = document.title;\r\n document.title = text;\r\n setTimeout(() => (document.title = backTitle), 3000);\r\n}\r\n\r\nexport default titleIndications;\r\n","/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\r\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\r\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\r\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\r\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\r\n var _, done = false;\r\n for (var i = decorators.length - 1; i >= 0; i--) {\r\n var context = {};\r\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\r\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\r\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\r\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\r\n if (kind === \"accessor\") {\r\n if (result === void 0) continue;\r\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\r\n if (_ = accept(result.get)) descriptor.get = _;\r\n if (_ = accept(result.set)) descriptor.set = _;\r\n if (_ = accept(result.init)) initializers.push(_);\r\n }\r\n else if (_ = accept(result)) {\r\n if (kind === \"field\") initializers.push(_);\r\n else descriptor[key] = _;\r\n }\r\n }\r\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\r\n done = true;\r\n};\r\n\r\nexport function __runInitializers(thisArg, initializers, value) {\r\n var useValue = arguments.length > 2;\r\n for (var i = 0; i < initializers.length; i++) {\r\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\r\n }\r\n return useValue ? value : void 0;\r\n};\r\n\r\nexport function __propKey(x) {\r\n return typeof x === \"symbol\" ? x : \"\".concat(x);\r\n};\r\n\r\nexport function __setFunctionName(f, name, prefix) {\r\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\r\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\r\n};\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n","class Logs {\r\n static question(text: string) {\r\n const css = \"color: cyan\";\r\n console.log(\"%c[QUESTION]: %s\", css, text);\r\n }\r\n\r\n static responseTry(text: string, valide: boolean) {\r\n const css = \"color: \" + (valide ? \"green\" : \"red\");\r\n console.log(\"%c[CHECKING]: %s\", css, text);\r\n }\r\n\r\n static array(arr: unknown[]) {\r\n console.log(\"[CORRECTS] \", arr);\r\n }\r\n\r\n static response(text: string) {\r\n console.log(text);\r\n }\r\n}\r\n\r\nexport default Logs;\r\n","/**\r\n * Normlize text\r\n * @param text\r\n */\r\nfunction normalizeText(text: string) {\r\n return text\r\n .replace(/\\n+/g, \"\\n\")\r\n .replace(/[ \\t]+/g, \" \")\r\n .toLowerCase()\r\n .trim()\r\n .replace(/^[a-z\\d]\\.\\s/gi, \"\") //a. text, b. text, c. text, 1. text, 2. text, 3.text\r\n .replace(/\\n[a-z\\d]\\.\\s/gi, \"\\n\"); //same but with new line\r\n}\r\n\r\nexport default normalizeText;\r\n","import Config from \"../types/config\";\r\nimport normalizeText from \"../utils/normalize-text\";\r\n\r\n/**\r\n * Get the response from chatGPT api\r\n * @param config\r\n * @param question\r\n * @returns\r\n */\r\nasync function getChatGPTResponse(\r\n config: Config,\r\n question: string\r\n): Promise {\r\n const req = await fetch(\"https://api.openai.com/v1/chat/completions\", {\r\n method: \"POST\",\r\n headers: {\r\n \"Content-Type\": \"application/json\",\r\n Authorization: `Bearer ${config.apiKey}`,\r\n },\r\n body: JSON.stringify({\r\n model:\r\n config.model && config.model !== \"\" ? config.model : \"gpt-3.5-turbo\",\r\n messages: [{ role: \"user\", content: question }],\r\n temperature: 0.8,\r\n top_p: 1.0,\r\n presence_penalty: 1.0,\r\n stop: null,\r\n }),\r\n });\r\n const rep = await req.json();\r\n const response = rep.choices[0].message.content;\r\n return normalizeText(response);\r\n}\r\n\r\nexport default getChatGPTResponse;\r\n","import Config from \"../types/config\";\r\nimport normalizeText from \"../utils/normalize-text\";\r\n\r\n/**\r\n * Normalize the question and add sub informations\r\n * @param langage\r\n * @param question\r\n * @returns\r\n */\r\nfunction normalizeQuestion(langage: Config[\"langage\"], question: string) {\r\n const finalQuestion = `Give a short response as possible for this question, reply in ${\r\n langage && langage !== \"\"\r\n ? 'this langage \"' + langage + '\"'\r\n : \"the following question langage\"\r\n } and only show the result: \r\n ${question} \r\n (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)`;\r\n return normalizeText(finalQuestion);\r\n}\r\n\r\nexport default normalizeQuestion;\r\n","import Config from \"../../types/config\";\r\nimport Logs from \"../../utils/logs\";\r\nimport normalizeText from \"../../utils/normalize-text\";\r\n\r\n/**\r\n * Handle checkbox and input elements\r\n * @param config\r\n * @param inputList\r\n * @param response\r\n */\r\nfunction handleRadioAndCheckbox(\r\n config: Config,\r\n inputList: NodeListOf,\r\n response: string\r\n): boolean {\r\n const input = inputList?.[0] as HTMLInputElement;\r\n\r\n if (!input || (input.type !== \"checkbox\" && input.type !== \"radio\"))\r\n return false;\r\n\r\n for (const input of inputList as NodeListOf) {\r\n const content = normalizeText(input.parentNode.textContent);\r\n const valide = response.includes(content);\r\n if (config.logs) Logs.responseTry(content, valide);\r\n if (valide) {\r\n if (config.mouseover) {\r\n input.addEventListener(\"mouseover\", () => (input.checked = true), {\r\n once: true,\r\n });\r\n } else {\r\n input.checked = true;\r\n }\r\n }\r\n }\r\n return true;\r\n}\r\n\r\nexport default handleRadioAndCheckbox;\r\n","import Config from \"../../types/config\";\r\nimport Logs from \"../../utils/logs\";\r\nimport normalizeText from \"../../utils/normalize-text\";\r\n\r\n/**\r\n * Handle select elements (and put in order select)\r\n * @param config\r\n * @param inputList\r\n * @param response\r\n * @returns\r\n */\r\nfunction handleSelect(\r\n config: Config,\r\n inputList: NodeListOf,\r\n response: string\r\n): boolean {\r\n if (inputList.length === 0 || inputList[0].tagName !== \"SELECT\") return false;\r\n\r\n let correct = response.split(\"\\n\");\r\n if (correct.length === 1 && correct.length !== inputList.length)\r\n correct = response.split(\",\");\r\n\r\n if (config.logs) Logs.array(correct);\r\n\r\n for (let j = 0; j < inputList.length; ++j) {\r\n const options = inputList[j].querySelectorAll(\"option\");\r\n\r\n for (const option of options) {\r\n const content = normalizeText(option.textContent);\r\n const valide = correct[j].includes(content);\r\n\r\n //if it's a put in order\r\n if (!isNaN(parseInt(content))) {\r\n const content = normalizeText(\r\n (option.parentNode as HTMLElement)\r\n .closest(\"tr\")\r\n .querySelector(\".text\").textContent\r\n );\r\n const index = correct.findIndex((c) => {\r\n const valide = c.includes(content);\r\n if (config.logs) Logs.responseTry(content, valide);\r\n return valide;\r\n });\r\n if (index !== -1) {\r\n if (config.mouseover) {\r\n options[index + 1].closest(\"select\").addEventListener(\r\n \"click\",\r\n function () {\r\n options[index + 1].selected = \"selected\" as any;\r\n },\r\n { once: true }\r\n );\r\n } else {\r\n options[index + 1].selected = \"selected\" as any;\r\n }\r\n break;\r\n }\r\n }\r\n //end put in order\r\n\r\n if (config.logs) Logs.responseTry(content, valide);\r\n\r\n if (valide) {\r\n if (config.mouseover) {\r\n option\r\n .closest(\"select\")\r\n .addEventListener(\"click\", () => (option.selected = true), {\r\n once: true,\r\n });\r\n } else {\r\n option.selected = true;\r\n }\r\n break;\r\n }\r\n }\r\n }\r\n\r\n return true;\r\n}\r\n\r\nexport default handleSelect;\r\n","import Config from \"../../types/config\";\r\n\r\n/**\r\n * Handle textbox\r\n * @param config\r\n * @param inputList\r\n * @param response\r\n * @returns\r\n */\r\nfunction handleTextbox(\r\n config: Config,\r\n inputList: NodeListOf,\r\n response: string\r\n): boolean {\r\n const input = inputList[0] as HTMLInputElement | HTMLTextAreaElement;\r\n\r\n if (\r\n inputList.length !== 1 ||\r\n (input.tagName !== \"TEXTAREA\" && input.type !== \"text\")\r\n )\r\n return false;\r\n\r\n if (config.typing) {\r\n let index = 0;\r\n input.addEventListener(\"keydown\", function (event: KeyboardEvent) {\r\n if (event.key === \"Backspace\") index = response.length + 1;\r\n if (index > response.length) return;\r\n event.preventDefault();\r\n input.value = response.slice(0, ++index);\r\n });\r\n } else {\r\n input.value = response;\r\n }\r\n\r\n return true;\r\n}\r\n\r\nexport default handleTextbox;\r\n","import Config from \"../../types/config\";\r\nimport titleIndications from \"../../utils/title-indications\";\r\n\r\n/**\r\n * Copy the response in the clipboard if we can automaticaly fill the question\r\n * @param config\r\n * @param inputList\r\n * @param response\r\n * @param force Force the copy to clipboard\r\n * @returns\r\n */\r\nfunction handleClipboard(config: Config, response: string) {\r\n if (config.title) titleIndications(\"Copied to clipboard\");\r\n navigator.clipboard.writeText(response);\r\n}\r\n\r\nexport default handleClipboard;\r\n","import Config from \"../types/config\";\r\nimport Logs from \"../utils/logs\";\r\nimport getChatGPTResponse from \"./get-response\";\r\nimport normalizeQuestion from \"./normalize-question\";\r\nimport handleRadioAndCheckbox from \"./questions/radio-checkbox\";\r\nimport handleSelect from \"./questions/select\";\r\nimport handleTextbox from \"./questions/textbox\";\r\nimport handleClipboard from \"./questions/clipboard\";\r\n\r\n/**\r\n * Reply to the question\r\n * @param config\r\n * @param hiddenButton\r\n * @param form\r\n * @param query\r\n * @returns\r\n */\r\nasync function reply(\r\n config: Config,\r\n hiddenButton: HTMLElement,\r\n form: HTMLElement,\r\n query: string\r\n) {\r\n if (config.cursor) hiddenButton.style.cursor = \"wait\";\r\n\r\n form.querySelector(\".accesshide\")?.remove();\r\n\r\n const question = normalizeQuestion(config.langage, form.textContent);\r\n const inputList: NodeListOf = form.querySelectorAll(query);\r\n\r\n const response = await getChatGPTResponse(config, question);\r\n\r\n if (config.logs) {\r\n Logs.question(question);\r\n Logs.response(response);\r\n }\r\n\r\n if (config.cursor)\r\n hiddenButton.style.cursor = config.infinite ? \"pointer\" : \"initial\";\r\n\r\n const handlers = [handleTextbox, handleSelect, handleRadioAndCheckbox];\r\n\r\n for (const handler of handlers) {\r\n if (handler(config, inputList, response)) return;\r\n }\r\n\r\n handleClipboard(config, response);\r\n}\r\n\r\nexport default reply;\r\n","import Config from \"../types/config\";\r\nimport titleIndications from \"../utils/title-indications\";\r\nimport reply from \"./reply\";\r\n\r\nconst pressedKeys: string[] = [];\r\nconst listeners: {\r\n element: HTMLElement;\r\n fn: (this: HTMLElement, ev: MouseEvent) => any;\r\n}[] = [];\r\n\r\n/**\r\n * Create a listener on the keyboard to inject the code\r\n * @param config\r\n */\r\nfunction codeListener(config: Config) {\r\n document.body.addEventListener(\"keydown\", function (event) {\r\n pressedKeys.push(event.key);\r\n if (pressedKeys.length > config.code.length) pressedKeys.shift();\r\n if (pressedKeys.join(\"\") === config.code) {\r\n pressedKeys.length = 0;\r\n setUpMoodleGpt(config);\r\n }\r\n });\r\n}\r\n\r\n/**\r\n * Setup moodleGPT into the page (remove/injection)\r\n * @param config\r\n * @returns\r\n */\r\nfunction setUpMoodleGpt(config: Config) {\r\n //removing events\r\n if (listeners.length > 0) {\r\n for (const listener of listeners) {\r\n if (config.cursor) listener.element.style.cursor = \"initial\";\r\n listener.element.removeEventListener(\"click\", listener.fn);\r\n }\r\n if (config.title) titleIndications(\"Removed\");\r\n listeners.length = 0;\r\n return;\r\n }\r\n\r\n //injection\r\n const inputQuery = [\"checkbox\", \"radio\", \"text\"]\r\n .map((e) => `input[type=\"${e}\"]`)\r\n .join(\",\");\r\n const query = inputQuery + \", textarea, select\";\r\n const forms = Array.from(document.querySelectorAll(\".formulation\"));\r\n\r\n for (const form of forms) {\r\n const hiddenButton: HTMLElement = form.querySelector(\".qtext\");\r\n if (config.cursor) hiddenButton.style.cursor = \"pointer\";\r\n const fn = reply.bind(null, config, hiddenButton, form, query);\r\n listeners.push({ element: hiddenButton, fn });\r\n hiddenButton.addEventListener(\"click\", fn, { once: !config.infinite });\r\n }\r\n\r\n if (config.title) titleIndications(\"Injected\");\r\n}\r\n\r\nexport default codeListener;\r\n","import codeListener from \"./core/code-listener\";\r\n\r\nchrome.storage.sync.get([\"moodleGPT\"]).then(function (storage) {\r\n const config = storage.moodleGPT;\r\n\r\n if (!config) throw new Error(\"Please configure MoodleGPT into the extension\");\r\n\r\n codeListener(config);\r\n});\r\n"],"names":[],"mappings":";;;;;EAAA;;;EAGG;EACH,SAAS,gBAAgB,CAAC,IAAY,EAAA;EACpC,IAAA,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC;EACjC,IAAA,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC;EACtB,IAAA,UAAU,CAAC,OAAO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AAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;EACxE,KAAA;MAED,IAAI,MAAM,CAAC,KAAK;UAAE,gBAAgB,CAAC,UAAU,CAAC,CAAC;EACjD;;ECxDA,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,OAAO,EAAA;EAC3D,IAAA,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;EAEjC,IAAA,IAAI,CAAC,MAAM;EAAE,QAAA,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;MAE9E,YAAY,CAAC,MAAM,CAAC,CAAC;EACvB,CAAC,CAAC;;;;;;","x_google_ignoreList":[1]} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..05a8e00 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "moodle-gpt", + "version": "1.0.0", + "description": "This extension allows you to hide CHAT-GPT in a Moodle quiz.", + "scripts": { + "build": "rollup -c" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/yoannchb-pro/MoodleGPT.git" + }, + "keywords": [ + "solve", + "moodle", + "extension-chrome", + "quiz-solutions", + "chatgpt" + ], + "author": "yoannchb", + "license": "MIT", + "bugs": { + "url": "https://github.com/yoannchb-pro/MoodleGPT/issues" + }, + "homepage": "https://github.com/yoannchb-pro/MoodleGPT#readme", + "devDependencies": { + "rollup": "^3.20.0", + "rollup-plugin-ts": "^3.2.0", + "typescript": "^5.0.2" + }, + "dependencies": { + "@types/chrome": "^0.0.224" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..67fe55d --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,15 @@ +const ts = require("rollup-plugin-ts"); + +const config = require("./tsconfig.json"); + +module.exports = { + input: "./src/index.ts", + output: [ + { + file: "./extension/moodle-gpt.js", + format: "umd", + sourcemap: true, + }, + ], + plugins: [ts(config)], +}; diff --git a/src/core/code-listener.ts b/src/core/code-listener.ts new file mode 100644 index 0000000..bb31e6e --- /dev/null +++ b/src/core/code-listener.ts @@ -0,0 +1,61 @@ +import Config from "../types/config"; +import titleIndications from "../utils/title-indications"; +import reply from "./reply"; + +const pressedKeys: string[] = []; +const listeners: { + element: HTMLElement; + fn: (this: HTMLElement, ev: MouseEvent) => any; +}[] = []; + +/** + * Create a listener on the keyboard to inject the code + * @param config + */ +function codeListener(config: Config) { + document.body.addEventListener("keydown", function (event) { + pressedKeys.push(event.key); + if (pressedKeys.length > config.code.length) pressedKeys.shift(); + if (pressedKeys.join("") === config.code) { + pressedKeys.length = 0; + setUpMoodleGpt(config); + } + }); +} + +/** + * Setup moodleGPT into the page (remove/injection) + * @param config + * @returns + */ +function setUpMoodleGpt(config: Config) { + //removing events + if (listeners.length > 0) { + for (const listener of listeners) { + if (config.cursor) listener.element.style.cursor = "initial"; + listener.element.removeEventListener("click", listener.fn); + } + if (config.title) titleIndications("Removed"); + listeners.length = 0; + return; + } + + //injection + const inputQuery = ["checkbox", "radio", "text"] + .map((e) => `input[type="${e}"]`) + .join(","); + const query = inputQuery + ", textarea, select"; + const forms = Array.from(document.querySelectorAll(".formulation")); + + 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 }); + hiddenButton.addEventListener("click", fn, { once: !config.infinite }); + } + + if (config.title) titleIndications("Injected"); +} + +export default codeListener; diff --git a/src/core/get-response.ts b/src/core/get-response.ts new file mode 100644 index 0000000..368d4f8 --- /dev/null +++ b/src/core/get-response.ts @@ -0,0 +1,35 @@ +import Config from "../types/config"; +import normalizeText from "../utils/normalize-text"; + +/** + * Get the response from chatGPT api + * @param config + * @param question + * @returns + */ +async function getChatGPTResponse( + config: Config, + question: string +): Promise { + const req = await fetch("https://api.openai.com/v1/chat/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${config.apiKey}`, + }, + body: JSON.stringify({ + model: + config.model && config.model !== "" ? config.model : "gpt-3.5-turbo", + messages: [{ role: "user", content: question }], + temperature: 0.8, + top_p: 1.0, + presence_penalty: 1.0, + stop: null, + }), + }); + const rep = await req.json(); + const response = rep.choices[0].message.content; + return normalizeText(response); +} + +export default getChatGPTResponse; diff --git a/src/core/normalize-question.ts b/src/core/normalize-question.ts new file mode 100644 index 0000000..e214ac7 --- /dev/null +++ b/src/core/normalize-question.ts @@ -0,0 +1,21 @@ +import Config from "../types/config"; +import normalizeText from "../utils/normalize-text"; + +/** + * Normalize the question and add sub informations + * @param langage + * @param question + * @returns + */ +function normalizeQuestion(langage: Config["langage"], question: string) { + const finalQuestion = `Give a short response as possible for this question, reply in ${ + langage && langage !== "" + ? 'this langage "' + langage + '"' + : "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; diff --git a/src/core/questions/clipboard.ts b/src/core/questions/clipboard.ts new file mode 100644 index 0000000..ba42572 --- /dev/null +++ b/src/core/questions/clipboard.ts @@ -0,0 +1,17 @@ +import Config from "../../types/config"; +import titleIndications from "../../utils/title-indications"; + +/** + * Copy the response in the clipboard if we can automaticaly fill the question + * @param config + * @param inputList + * @param response + * @param force Force the copy to clipboard + * @returns + */ +function handleClipboard(config: Config, response: string) { + if (config.title) titleIndications("Copied to clipboard"); + navigator.clipboard.writeText(response); +} + +export default handleClipboard; diff --git a/src/core/questions/radio-checkbox.ts b/src/core/questions/radio-checkbox.ts new file mode 100644 index 0000000..65c9a79 --- /dev/null +++ b/src/core/questions/radio-checkbox.ts @@ -0,0 +1,38 @@ +import Config from "../../types/config"; +import Logs from "../../utils/logs"; +import normalizeText from "../../utils/normalize-text"; + +/** + * Handle checkbox and input elements + * @param config + * @param inputList + * @param response + */ +function handleRadioAndCheckbox( + config: Config, + inputList: NodeListOf, + response: string +): boolean { + const input = inputList?.[0] as HTMLInputElement; + + if (!input || (input.type !== "checkbox" && input.type !== "radio")) + return false; + + for (const input of inputList as NodeListOf) { + const content = normalizeText(input.parentNode.textContent); + const valide = response.includes(content); + if (config.logs) Logs.responseTry(content, valide); + if (valide) { + if (config.mouseover) { + input.addEventListener("mouseover", () => (input.checked = true), { + once: true, + }); + } else { + input.checked = true; + } + } + } + return true; +} + +export default handleRadioAndCheckbox; diff --git a/src/core/questions/select.ts b/src/core/questions/select.ts new file mode 100644 index 0000000..0b65e36 --- /dev/null +++ b/src/core/questions/select.ts @@ -0,0 +1,81 @@ +import Config from "../../types/config"; +import Logs from "../../utils/logs"; +import normalizeText from "../../utils/normalize-text"; + +/** + * Handle select elements (and put in order select) + * @param config + * @param inputList + * @param response + * @returns + */ +function handleSelect( + config: Config, + inputList: NodeListOf, + response: string +): boolean { + if (inputList.length === 0 || inputList[0].tagName !== "SELECT") return false; + + let correct = response.split("\n"); + if (correct.length === 1 && correct.length !== inputList.length) + correct = response.split(","); + + if (config.logs) Logs.array(correct); + + for (let j = 0; j < inputList.length; ++j) { + const options = inputList[j].querySelectorAll("option"); + + for (const option of options) { + const content = normalizeText(option.textContent); + const valide = correct[j].includes(content); + + //if it's a put in order + if (!isNaN(parseInt(content))) { + const content = normalizeText( + (option.parentNode as HTMLElement) + .closest("tr") + .querySelector(".text").textContent + ); + const index = correct.findIndex((c) => { + const valide = c.includes(content); + if (config.logs) Logs.responseTry(content, valide); + return valide; + }); + if (index !== -1) { + if (config.mouseover) { + options[index + 1].closest("select").addEventListener( + "click", + function () { + options[index + 1].selected = "selected" as any; + }, + { once: true } + ); + } else { + options[index + 1].selected = "selected" as any; + } + break; + } + } + //end put in order + + if (config.logs) Logs.responseTry(content, valide); + + if (valide) { + if (config.mouseover) { + option + .closest("select") + .addEventListener("click", () => (option.selected = true), { + once: true, + }); + } else { + option.selected = true; + } + break; + } + } + } + + return true; +} + +export default handleSelect; diff --git a/src/core/questions/textbox.ts b/src/core/questions/textbox.ts new file mode 100644 index 0000000..8b226d1 --- /dev/null +++ b/src/core/questions/textbox.ts @@ -0,0 +1,38 @@ +import Config from "../../types/config"; + +/** + * Handle textbox + * @param config + * @param inputList + * @param response + * @returns + */ +function handleTextbox( + config: Config, + inputList: NodeListOf, + response: string +): boolean { + const input = inputList[0] as HTMLInputElement | HTMLTextAreaElement; + + if ( + inputList.length !== 1 || + (input.tagName !== "TEXTAREA" && input.type !== "text") + ) + return false; + + if (config.typing) { + let index = 0; + input.addEventListener("keydown", function (event: KeyboardEvent) { + if (event.key === "Backspace") index = response.length + 1; + if (index > response.length) return; + event.preventDefault(); + input.value = response.slice(0, ++index); + }); + } else { + input.value = response; + } + + return true; +} + +export default handleTextbox; diff --git a/src/core/reply.ts b/src/core/reply.ts new file mode 100644 index 0000000..56a987e --- /dev/null +++ b/src/core/reply.ts @@ -0,0 +1,50 @@ +import Config from "../types/config"; +import Logs from "../utils/logs"; +import getChatGPTResponse from "./get-response"; +import normalizeQuestion from "./normalize-question"; +import handleRadioAndCheckbox from "./questions/radio-checkbox"; +import handleSelect from "./questions/select"; +import handleTextbox from "./questions/textbox"; +import handleClipboard from "./questions/clipboard"; + +/** + * Reply to the question + * @param config + * @param hiddenButton + * @param form + * @param query + * @returns + */ +async function reply( + config: Config, + hiddenButton: HTMLElement, + form: HTMLElement, + query: string +) { + if (config.cursor) hiddenButton.style.cursor = "wait"; + + form.querySelector(".accesshide")?.remove(); + + const question = normalizeQuestion(config.langage, form.textContent); + const inputList: NodeListOf = form.querySelectorAll(query); + + const response = await getChatGPTResponse(config, question); + + if (config.logs) { + Logs.question(question); + Logs.response(response); + } + + if (config.cursor) + hiddenButton.style.cursor = config.infinite ? "pointer" : "initial"; + + const handlers = [handleTextbox, handleSelect, handleRadioAndCheckbox]; + + for (const handler of handlers) { + if (handler(config, inputList, response)) return; + } + + handleClipboard(config, response); +} + +export default reply; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..10e0694 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,9 @@ +import codeListener from "./core/code-listener"; + +chrome.storage.sync.get(["moodleGPT"]).then(function (storage) { + const config = storage.moodleGPT; + + if (!config) throw new Error("Please configure MoodleGPT into the extension"); + + codeListener(config); +}); diff --git a/src/types/config.d.ts b/src/types/config.d.ts new file mode 100644 index 0000000..ad0b704 --- /dev/null +++ b/src/types/config.d.ts @@ -0,0 +1,14 @@ +type Config = { + apiKey: string; + code: string; + langage?: string; + model?: string; + infinite?: boolean; + typing?: boolean; + mouseover?: boolean; + cursor?: boolean; + logs?: boolean; + title?: boolean; +}; + +export default Config; diff --git a/src/utils/logs.ts b/src/utils/logs.ts new file mode 100644 index 0000000..d9069dd --- /dev/null +++ b/src/utils/logs.ts @@ -0,0 +1,21 @@ +class Logs { + static question(text: string) { + const css = "color: cyan"; + console.log("%c[QUESTION]: %s", css, text); + } + + static responseTry(text: string, valide: boolean) { + const css = "color: " + (valide ? "green" : "red"); + console.log("%c[CHECKING]: %s", css, text); + } + + static array(arr: unknown[]) { + console.log("[CORRECTS] ", arr); + } + + static response(text: string) { + console.log(text); + } +} + +export default Logs; diff --git a/src/utils/normalize-text.ts b/src/utils/normalize-text.ts new file mode 100644 index 0000000..9a82cd4 --- /dev/null +++ b/src/utils/normalize-text.ts @@ -0,0 +1,15 @@ +/** + * Normlize text + * @param text + */ +function normalizeText(text: string) { + return text + .replace(/\n+/g, "\n") + .replace(/[ \t]+/g, " ") + .toLowerCase() + .trim() + .replace(/^[a-z\d]\.\s/gi, "") //a. text, b. text, c. text, 1. text, 2. text, 3.text + .replace(/\n[a-z\d]\.\s/gi, "\n"); //same but with new line +} + +export default normalizeText; diff --git a/src/utils/title-indications.ts b/src/utils/title-indications.ts new file mode 100644 index 0000000..65c7506 --- /dev/null +++ b/src/utils/title-indications.ts @@ -0,0 +1,11 @@ +/** + * Show some informations into the document title and remove it after 3000ms + * @param text + */ +function titleIndications(text: string) { + const backTitle = document.title; + document.title = text; + setTimeout(() => (document.title = backTitle), 3000); +} + +export default titleIndications; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..c7d476b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "CommonJS", + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "target": "ES6", + "noImplicitAny": true, + "moduleResolution": "node", + "sourceMap": true, + "outDir": "extension", + "resolveJsonModule": true, + "types": ["node", "chrome"], + "typeRoots": ["node_modules/@types"] + }, + "include": ["src/**/*"] +}