diff --git a/awnser.js b/awnser.js new file mode 100644 index 0000000..cd65643 --- /dev/null +++ b/awnser.js @@ -0,0 +1,63 @@ +function awnserQuestion(awnserData){ + const question = document.querySelector(".question:not(.hidden)"); + if (!question) { + return; + } + + const questionTextDom = question.querySelector(".questionText .mattext"); + if (!questionTextDom) return; + const questionText = questionTextDom.textContent.trim(); + + const answersDom = question.querySelector("ul.coreContent"); + if (!answersDom) return; + const answers = answersDom.children; + + for (let answer of answers) { + const input = answer.querySelector("input"); + if (!input) continue; + input.checked = false; + } + + const correctAnswers = findAnswers(awnserData, questionText, answers); + if (correctAnswers.length === 0) { + GM_log("no awnser") + return; + } + + for (const answer of correctAnswers) { + const input = answer.querySelector("input"); + if (!input) continue; + input.checked = true; + } +} + +function findAnswers(awnserData, questionText, answers) { + if (awnserData === null) { + alert("No chapter data loaded. Maybe the fetch failed?!"); + return []; + } + + const correctAnswers = []; + for (let entry of awnserData) { + if (matchAwnser(questionText.trim(), entry.question.trim())) { + for (let availableAnswer of answers) { + for (let possibleAnswer of entry.answers) { + if (matchAwnser(availableAnswer.textContent.trim(), possibleAnswer)) { + correctAnswers.push(availableAnswer); + } + } + } + } + } + + return correctAnswers; +} + +function matchAwnser(textA, textB) { + const replaceRegex = /[^\w]/gi; + textA = textA.replace(replaceRegex, ""); + textB = textB.replace(replaceRegex, ""); + return (textA === textB); +} + +window.awnserQuestion = awnserQuestion; \ No newline at end of file diff --git a/fetch.user.js b/fetch.js similarity index 100% rename from fetch.user.js rename to fetch.js diff --git a/main.user.js b/main.user.js index b113db5..4e8efdf 100644 --- a/main.user.js +++ b/main.user.js @@ -5,12 +5,13 @@ // @match *://www.assessment.netacad.net/* // @match *://www.google.com/* // @match *://www.google.de/* -// @require https://git.euph.dev/SZUT-Dominik/CCNA_Autofill_Userscript/raw/branch/main/fetch.user.js +// @require https://git.euph.dev/SZUT-Dominik/CCNA_Autofill_Userscript/raw/branch/main/fetch.js +// @require https://git.euph.dev/SZUT-Dominik/CCNA_Autofill_Userscript/raw/branch/main/awnser.js // @grant GM_setValue // @grant GM_getValue // @grant GM_xmlhttpRequest // @grant GM_log -// @version 0.0.10 +// @version 0.0.11 // @author Dominik Säume // ==/UserScript== @@ -33,69 +34,7 @@ window.addEventListener("keydown", async (event) => { break; case "a": - awnserQuestion(); + window.awnserQuestion(awnserData); break; } -}) - -function awnserQuestion(){ - const question = document.querySelector(".question:not(.hidden)"); - if (!question) { - return; - } - - const questionTextDom = question.querySelector(".questionText .mattext"); - if (!questionTextDom) return; - const questionText = questionTextDom.textContent.trim(); - - const answersDom = question.querySelector("ul.coreContent"); - if (!answersDom) return; - const answers = answersDom.children; - - for (let answer of answers) { - const input = answer.querySelector("input"); - if (!input) continue; - input.checked = false; - } - - const correctAnswers = findAnswers(questionText, answers); - if (correctAnswers.length === 0) { - GM_log("no awnser") - return; - } - - for (const answer of correctAnswers) { - const input = answer.querySelector("input"); - if (!input) continue; - input.checked = true; - } -} - -function findAnswers(questionText, answers) { - if (awnserData === null) { - alert("No chapter data loaded. Maybe the fetch failed?!"); - return []; - } - - const correctAnswers = []; - for (let entry of awnserData) { - if (matchAwnser(questionText.trim(), entry.question.trim())) { - for (let availableAnswer of answers) { - for (let possibleAnswer of entry.answers) { - if (matchAwnser(availableAnswer.textContent.trim(), possibleAnswer)) { - correctAnswers.push(availableAnswer); - } - } - } - } - } - - return correctAnswers; -} - -function matchAwnser(textA, textB) { - const replaceRegex = /[^\w]/gi; - textA = textA.replace(replaceRegex, ""); - textB = textB.replace(replaceRegex, ""); - return (textA === textB); -} \ No newline at end of file +}); \ No newline at end of file