1
1
Fork 0
CCNA_Autofill_Userscript/main.user.js

101 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-05-23 14:02:37 +00:00
// ==UserScript==
// @name CCNA Autofill
// @namespace Violentmonkey Scripts
// @match *://assessment.netacad.net/*
// @match *://www.assessment.netacad.net/*
2024-05-23 14:11:32 +00:00
// @match *://www.google.com/*
// @match *://www.google.de/*
2024-05-23 14:17:43 +00:00
// @require https://git.euph.dev/SZUT-Dominik/CCNA_Autofill_Userscript/raw/branch/main/fetch.user.js
2024-05-23 14:02:37 +00:00
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_log
2024-05-23 14:28:50 +00:00
// @version 0.0.8
2024-05-23 14:02:37 +00:00
// @author Dominik Säume
2024-05-23 14:06:28 +00:00
// ==/UserScript==
2024-05-23 14:11:32 +00:00
2024-05-23 14:06:28 +00:00
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);
2024-05-23 14:28:41 +00:00
awnserData = await window.fetchAwnsers(newAwnsersURL);
2024-05-23 14:06:28 +00:00
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);
}