1
1
Fork 0
CCNA_Autofill_Userscript/answer.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-05-23 15:38:36 +00:00
/**
*
* @param {Array<Answer>} answerData
* @returns
*/
2024-05-23 15:34:40 +00:00
function answerQuestion(answerData){
2024-05-23 14:36:13 +00:00
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;
2024-05-23 15:34:40 +00:00
for (let answer of Array.from(answers)) {
2024-05-23 14:36:13 +00:00
const input = answer.querySelector("input");
if (!input) continue;
input.checked = false;
}
2024-05-23 15:34:40 +00:00
const correctAnswers = findAnswers(answerData, questionText, answers);
2024-05-23 14:36:13 +00:00
if (correctAnswers.length === 0) {
return;
}
for (const answer of correctAnswers) {
const input = answer.querySelector("input");
if (!input) continue;
input.checked = true;
}
}
2024-05-23 15:38:36 +00:00
/**
*
* @param {Array<Answer>} answerData
* @param {string} questionText
* @returns
*/
2024-05-23 15:34:40 +00:00
function findAnswers(answerData, questionText, answers) {
if (answerData === null) {
2024-05-23 14:36:13 +00:00
return [];
}
const correctAnswers = [];
2024-05-23 15:34:40 +00:00
for (let entry of answerData) {
if (matchAnswer(questionText.trim(), entry.question.trim())) {
2024-05-23 14:36:13 +00:00
for (let availableAnswer of answers) {
for (let possibleAnswer of entry.answers) {
2024-05-23 15:34:40 +00:00
if (matchAnswer(availableAnswer.textContent.trim(), possibleAnswer)) {
2024-05-23 14:36:13 +00:00
correctAnswers.push(availableAnswer);
}
}
}
}
}
return correctAnswers;
}
2024-05-23 15:34:40 +00:00
function matchAnswer(textA, textB) {
2024-05-23 14:36:13 +00:00
const replaceRegex = /[^\w]/gi;
textA = textA.replace(replaceRegex, "");
textB = textB.replace(replaceRegex, "");
return (textA === textB);
}
2024-05-23 15:34:40 +00:00
window.answerQuestion = answerQuestion;