1
1
Fork 0
This commit is contained in:
Dominik Säume 2024-05-23 16:36:13 +02:00
parent b10972bd0d
commit d7a20264fb
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
3 changed files with 68 additions and 66 deletions

63
awnser.js Normal file
View file

@ -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;

View file

@ -5,12 +5,13 @@
// @match *://www.assessment.netacad.net/* // @match *://www.assessment.netacad.net/*
// @match *://www.google.com/* // @match *://www.google.com/*
// @match *://www.google.de/* // @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_setValue
// @grant GM_getValue // @grant GM_getValue
// @grant GM_xmlhttpRequest // @grant GM_xmlhttpRequest
// @grant GM_log // @grant GM_log
// @version 0.0.10 // @version 0.0.11
// @author Dominik Säume // @author Dominik Säume
// ==/UserScript== // ==/UserScript==
@ -33,69 +34,7 @@ window.addEventListener("keydown", async (event) => {
break; break;
case "a": case "a":
awnserQuestion(); window.awnserQuestion(awnserData);
break; 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);
}