1
1
Fork 0
CCNA_Autofill_Userscript/main.user.js

103 lines
No EOL
2.9 KiB
JavaScript

// ==UserScript==
// @name CCNA Autofill
// @namespace Violentmonkey Scripts
// @match *://assessment.netacad.net/*
// @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
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_log
// @version 0.0.8
// @author Dominik Säume
// ==/UserScript==
window.hello();
const URL_STORAGE_KEY = "itexamanswers.net URL";
let awnserData;
console.log = console.__proto__.log;
window.addEventListener("keydown", event => {
switch(event.key){
case "p":
const oldAwnsersURL = GM_getValue(URL_STORAGE_KEY);
let newAwnsersURL = prompt("Please input the answer url (itexamanswers.net)", oldAwnsersURL);
GM_setValue(URL_STORAGE_KEY, newAwnsersURL);
awnserData = await window.fetchAwnsers(newAwnsersURL);
break;
case "n":
document.getElementById("next").click();
break;
case "a":
awnserQuestion();
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);
}