diff --git a/CHANGELOG b/CHANGELOG index 6b44aca..8ce9856 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,3 +9,5 @@ Passive mode 1.6.1.6: Fixed question saving match percent +1.6.2.0: + Document element getting refactoring diff --git a/frame.js b/frame.js index f48f6b8..0070f52 100644 --- a/frame.js +++ b/frame.js @@ -21,7 +21,7 @@ // ==UserScript== // @name Moodle/Elearning/KMOOC test help -// @version 1.6.2.7 +// @version 1.6.3.0 // @description Online Moodle/Elearning/KMOOC test help // @author YourFriendlyNeighborhoodDealer // @match https://elearning.uni-obuda.hu/main/* diff --git a/main.js b/main.js index 78b372f..18c6f25 100644 --- a/main.js +++ b/main.js @@ -22,7 +22,7 @@ var data; // all data, which is in the resource txt var addEventListener; // add event listener function -const lastChangeLog = ''; // TODO +const lastChangeLog = 'Néhány szerkezeti átalakítás, és bugfix. Ha valami elromlott akkor pls report, thanx'; const serverAdress = "https://qmining.tk/"; // forcing pages for testing. unless you test, do not set these to true! diff --git a/stable.js b/stable.js index 7b776cd..6a007a3 100644 --- a/stable.js +++ b/stable.js @@ -21,7 +21,7 @@ // ==UserScript== // @name Moodle/Elearning/KMOOC test help -// @version 1.6.2.7 +// @version 1.6.3.0 // @description Online Moodle/Elearning/KMOOC test help // @author YourFriendlyNeighborhoodDealer // @match https://elearning.uni-obuda.hu/main/* @@ -44,7 +44,7 @@ var data; // all data, which is in the resource txt var addEventListener; // add event listener function const lastChangeLog = - '- Apró, de lényeges fixek\n - Ha találkoztok bugokkal, akkor pls report! thanx'; + 'Néhány szerkezeti átalakítás, és bugfix. Ha valami elromlott akkor pls report, thanx'; const serverAdress = "https://qmining.tk/"; // forcing pages for testing. unless you test, do not set these to true! @@ -55,13 +55,13 @@ const logElementGetting = false; const log = false; - const motdShowCount = 3; + const motdShowCount = 3; /* Ammount of times to show motd */ var motd = ""; var lastestVersion = ""; - const minMatchAmmount = 55; - const minResultMatchPercent = 99; - const lengthDiffMultiplier = 10; + const minMatchAmmount = 60; /* Minimum ammount to consider that two questions match during answering */ + const minResultMatchPercent = 99; /* Minimum ammount to consider that two questions match during saving */ + const lengthDiffMultiplier = 10; /* Percent minus for length difference */ //: Class descriptions {{{ class Question { @@ -245,6 +245,427 @@ } //: }}} + //: DOM getting stuff {{{ + // all dom getting stuff are in this sections, so on + // moodle dom change, stuff breaks here + + class QuestionsPageModell { + + GetAllQuestionsDropdown() { + if (logElementGetting) + Log("getting dropdown question"); + let items = document.getElementById("responseform").getElementsByTagName("p")[0].childNodes; + let r = ""; + items.forEach((item) => { + if (item.tagName == undefined) + r += item.nodeValue; + + }); + return r; + } + + GetAllQuestionsQtext() { + if (logElementGetting) + Log("getting all questions qtext"); + return document.getElementById("responseform").getElementsByClassName("qtext"); // getting questions + } + + GetAllQuestionsP() { + if (logElementGetting) + Log("getting all questions by tag p"); + return document.getElementById("responseform").getElementsByTagName("p"); + } + + GetFormulationClearfix() { + if (logElementGetting) + Log("getting formulation clearfix lol"); + return document.getElementsByClassName("formulation clearfix"); + } + + GetAnswerOptions() { + if (logElementGetting) + Log("getting all answer options"); + return this.GetFormulationClearfix()[0].childNodes[3].innerText; + } + + GetQuestionImages() { + if (logElementGetting) + Log("getting question images"); + return this.GetFormulationClearfix()[0].getElementsByTagName("img"); + } + + // this function should return the question, posible answers, and image names + GetQuestionFromTest() { + var questions; // the important questions + var allQuestions; // all questions + try // trying to get tha current questions + { + allQuestions = this.GetAllQuestionsQtext(); // getting questions + if (allQuestions.length == 0) // if there are no found questions + { + var ddq = this.GetAllQuestionsDropdown(); + if (EmptyOrWhiteSpace(ddq)) { + var questionData = ""; + for (var j = 0; j < allQuestions.length; j++) { + // TODO: test dis + let subAllQuestions = allQuestions[j].childNodes; + for (var i = 0; i < subAllQuestions.length; i++) { + if (subAllQuestions[i].data != undefined && !EmptyOrWhiteSpace(subAllQuestions[i].data)) // if the current element has some text data to add + { + questionData += subAllQuestions[i].data + " "; // adding text to question data + } + } + } + questions = [questionData]; + } else + questions = [ddq]; + } else // if there is + { + questions = []; + for (var i = 0; i < allQuestions.length; i++) { + questions.push(allQuestions[i].innerText); + } + } + } catch (e) { + Exception(e, "script error at getting question:"); + } + var imgNodes = ""; // the image nodes for questions + try { + imgNodes = this.GetQuestionImages(); // getting question images, if there is any + AddImageNamesToImages(imgNodes); // adding image names to images, so its easier to search for, or even guessing + } catch (e) { + Log(e); + Log("Some error with images"); + } + + questions = questions.map((item, ind) => { + return ReplaceCharsWithSpace(item, "\n"); + }); + + return { + imgnodes: imgNodes, + allQ: allQuestions, + q: questions + }; + } + } + + class ResultsPageModell { + + DetermineQuestionType(nodes) { + let qtype = ""; + let i = 0; + + while (i < nodes.length && qtype == "") { + let inps = nodes[i].getElementsByTagName("input"); + + if (inps.length > 0) { + qtype = inps[0].type; + } + + i++; + } + + return qtype; + + } + + GetSelectAnswer() { + if (logElementGetting) + Log("getting selected answer"); + var t = document.getElementsByTagName("select"); + if (t.length > 0) + return t[0].options[document.getElementsByTagName("select")[ + 0].selectedIndex].innerText; + } + + GetCurrQuestion(i) { + if (logElementGetting) + Log("getting curr questions by index: " + i); + return document.getElementsByTagName("form")[0].childNodes[0].childNodes[i].childNodes[1].childNodes[ + 0].innerText; + } + + GetFormResult() { + if (logElementGetting) + Log("getting form result"); + var t = document.getElementsByTagName("form")[0].childNodes[0].childNodes; + if (t.length > 0 && t[0].tagName == undefined) // debreceni moodle + return document.getElementsByTagName("form")[1].childNodes[0].childNodes; + else + return t; + } + + GetAnswerNode(i) { + if (logElementGetting) + Log("getting answer node"); + + var results = this.GetFormResult(); // getting results element + + var r = results[i].getElementsByClassName("answer")[0].childNodes; + var ret = []; + for (var j = 0; j < r.length; j++) + if (r[j].tagName != undefined && r[j].tagName.toLowerCase() == "div") + ret.push(r[j]); + + let qtype = this.DetermineQuestionType(ret); + + return { + nodes: ret, + type: qtype + }; + } + + GetCurrentAnswer(i) { + if (logElementGetting) + Log("getting curr answer by index: " + i); + var results = this.GetFormResult(); // getting results element + var t = results[i].getElementsByClassName("formulation clearfix")[0].getElementsByTagName( + "span"); + if (t.length > 2) + return t[1].innerHTML.split("
")[1]; + } + + GetQText(i) { + if (logElementGetting) + Log("getting qtext by index: " + i); + var results = this.GetFormResult(); // getting results element + return results[i].getElementsByClassName("qtext"); + } + + GetDropboxes(i) { + if (logElementGetting) + Log("getting dropboxes by index: " + i); + var results = this.GetFormResult(); // getting results element + return results[i].getElementsByTagName("select"); + } + + GetAllAnswer(index) { + if (logElementGetting) + Log("getting all answers, ind: " + index); + return document.getElementsByClassName("answer")[index].childNodes; + } + + GetPossibleAnswers(i) { + if (logElementGetting) + Log("getting possible answers"); + var results = this.GetFormResult(); // getting results element + var items = results[i].getElementsByTagName("label"); + var r = []; + for (var j = 0; j < items.length; j++) { + + function TryGetCorrect(j) { + var cn = items[j].parentNode.className; + if (cn.includes("correct")) + return cn.includes("correct") && !cn.includes("incorrect"); + } + r.push({ + value: items[j].innerText, + iscorrect: TryGetCorrect(j) + }); + } + return r; + } + + GetRightAnswerIfCorrectShown(i) { + if (logElementGetting) + Log("getting right answer if correct shown"); + var results = this.GetFormResult(); // getting results element + return results[i].getElementsByClassName("rightanswer"); + } + + GetWrongAnswerIfCorrectNotShown(i) { + if (logElementGetting) + Log("getting wrong answer if correct not shown"); + var results = this.GetFormResult(); // getting results element + var n = results[i].getElementsByTagName("i")[0].parentNode; + if (n.className.includes("incorrect")) + return results[i].getElementsByTagName("i")[0].parentNode.innerText; + else + return ""; + } + + GetRightAnswerIfCorrectNotShown(i) { + if (logElementGetting) + Log("Getting right answer if correct not shown"); + var results = this.GetFormResult(); // getting results element + var n = results[i].getElementsByTagName("i")[0].parentNode; + if (n.className.includes("correct") && !n.className.includes("incorrect")) + return results[i].getElementsByTagName("i")[0].parentNode.innerText; + else + return ""; + } + + GetFormCFOfResult(result) { + if (logElementGetting) + Log("getting formulation clearfix"); + return result.getElementsByClassName("formulation clearfix")[0]; + } + + GetResultText(i) { + if (logElementGetting) + Log("getting result text"); + var results = this.GetFormResult(); // getting results element + return this.GetFormCFOfResult(results[i]).getElementsByTagName("p"); + } + + GetResultImage(i) { + if (logElementGetting) + Log("getting result image"); + var results = this.GetFormResult(); // getting results element + return this.GetFormCFOfResult(results[i]).getElementsByTagName("img"); + } + + + // gets the question from the result page + // i is the index of the question + GetQuestionFromResult(i) { + var temp = this.GetQText(i); + var currQuestion = ""; + if (temp.length > 0) // if the qtext element is not 0 length + { + currQuestion = temp[0].innerText; // adding the question to curr question as .q + } else { + // this is black magic fuckery a bit + if (this.GetDropboxes(i).length > 0) // if there is dropdown list in the current question + { + var allNodes = this.GetResultText(i); + currQuestion = ""; + for (var k = 0; k < allNodes.length; k++) { + var allQuestions = this.GetResultText(i)[k].childNodes; + for (var j = 0; j < allQuestions.length; j++) { + if (allQuestions[j].data != undefined && !EmptyOrWhiteSpace(allQuestions[j].data)) { + currQuestion += allQuestions[j].data + " "; + } + } + } + } else { + try { + currQuestion = this.GetCurrQuestion(i); + } catch (e) { + currQuestion = "REEEEEEEEEEEEEEEEEEEEE"; // this shouldnt really happen sry guys + Log("Unable to get question in GetQuestionFromResult"); + } + } + } + return currQuestion; + } + + // tries to get right answer from result page + // i is the index of the question + GetRightAnswerFromResult(i) { + var fun = []; + + // the basic type of getting answers + fun.push(function TryGet0(i) { + var temp = RPM.GetRightAnswerIfCorrectShown(i); // getting risht answer + if (temp.length > 0) // if the rightanswer element is not 0 length + return temp[0].innerText; // adding the answer to curr question as .a + }); + + // if there is dropdown list in the current question + fun.push(function TryGet1(i) { + if (RPM.GetDropboxes(i).length > 0) + return RPM.GetCurrentAnswer(i); + }); + + // if the correct answers are not shown, and the selected answer + // is correct + fun.push(function TryGet2(i) { + return RPM.GetRightAnswerIfCorrectNotShown(i); + }); + + // if there is dropbox in the question + fun.push(function TryGet3(i) { + return RPM.GetSelectAnswer(); + }); + + // if the correct answers are not shown, and the selected answer + // is incorrect, and there are only 2 options + fun.push(function TryGet4(i) { + var possibleAnswers = RPM.GetPossibleAnswers(i); + if (possibleAnswers.length == 2) { + for (var k = 0; k < possibleAnswers.length; k++) + if (possibleAnswers[k].iscorrect == undefined) + return possibleAnswers[k].value; + } + }); + + fun.push(function TryGetFinal(i) { + return undefined; + }); + + var j = 0; + var currAnswer; + while (j < fun.length && EmptyOrWhiteSpace(currAnswer)) { + currAnswer = fun[j](i); + j++; + } + + return currAnswer; + } + + // version 2 of getting right answer from result page + // i is the index of the question + GetRightAnswerFromResultv2(i) { + try { + var answerNodes = this.GetAnswerNode(i); + let items = answerNodes.nodes; + + if (answerNodes.type == "checkbox") + return GetRightAnswerFromResult(i); + + for (var j = 0; j < items.length; j++) { + var cn = items[j].className; + if (cn.includes("correct") && !cn.includes("incorrect")) + return items[j].innerText; + } + if (items.length == 2) { + for (var j = 0; j < items.length; j++) { + var cn = items[j].className; + if (!cn.includes("correct")) + return items[j].innerText; + } + } + } catch (e) { + Log("error at new nodegetting, trying the oldschool way"); + } + } + } + + class MiscPageModell { + + GetCurrentSubjectName() { + if (logElementGetting) + Log("getting current subjects name"); + return document.getElementById("page-header").innerText.split("\n")[0]; + } + + GetVideo() { + if (logElementGetting) + Log("getting video stuff"); + return document.getElementsByTagName("video")[0]; + } + + GetVideoElement() { + if (logElementGetting) + Log("getting video element"); + return document.getElementById("videoElement").parentNode; + } + + GetInputType(answers, i) { + if (logElementGetting) + Log("getting input type"); + return answers[i].getElementsByTagName("input")[0].type; + } + } + + var QPM = new QuestionsPageModell(); + var RPM = new ResultsPageModell(); + var MPM = new MiscPageModell(); + + //: }}} + Main(); //: Main function {{{ @@ -277,7 +698,7 @@ m: "Fatál error. Check console (f12). Kattints az üzenetre az összes kérdés/válaszért manuális kereséshez!", isSimple: true }, undefined, function() { - GM_openInTab(serverAdress + 'legacy', { + GM_openInTab(serverAdress + 'lred', { active: true }); }); @@ -295,414 +716,11 @@ console.timeEnd('main'); if (forceTestPage || forceResultPage || forceDefaultPage) - alert("TEST MODE"); + if (document.getElementById("scriptMessage")) + document.getElementById("scriptMessage").style.background = "green"; } //: }}} - //: DOM getting stuff {{{ - // all dom getting stuff are in this sections, so on - // moodle dom change, stuff breaks here - - function GetAllQuestionsDropdown() { - if (logElementGetting) - Log("getting dropdown question"); - let items = document.getElementById("responseform").getElementsByTagName("p")[0].childNodes; - let r = ""; - items.forEach((item) => { - if (item.tagName == undefined) - r += item.nodeValue; - - }); - return r; - } - - function GetAllQuestionsQtext() { - if (logElementGetting) - Log("getting all questions qtext"); - return document.getElementById("responseform").getElementsByClassName("qtext"); // getting questions - } - - function GetAllQuestionsP() { - if (logElementGetting) - Log("getting all questions by tag p"); - return document.getElementById("responseform").getElementsByTagName("p"); - } - - function GetFormulationClearfix() { - if (logElementGetting) - Log("getting formulation clearfix lol"); - return document.getElementsByClassName("formulation clearfix"); - } - - function GetAnswerOptions() { - if (logElementGetting) - Log("getting all answer options"); - return GetFormulationClearfix()[0].childNodes[3].innerText; - } - - function GetQuestionImages() { - if (logElementGetting) - Log("getting question images"); - return GetFormulationClearfix()[0].getElementsByTagName("img"); - } - - function GetCurrentSubjectName() { - if (logElementGetting) - Log("getting current subjects name"); - return document.getElementById("page-header").innerText.split("\n")[0]; - } - - function GetVideo() { - if (logElementGetting) - Log("getting video stuff"); - return document.getElementsByTagName("video")[0]; - } - - function GetVideoElement() { - if (logElementGetting) - Log("getting video element"); - return document.getElementById("videoElement").parentNode; - } - - function GetAllAnswer(index) { - if (logElementGetting) - Log("getting all answers, ind: " + index); - return document.getElementsByClassName("answer")[index].childNodes; - } - - function GetInputType(answers, i) { - if (logElementGetting) - Log("getting input type"); - return answers[i].getElementsByTagName("input")[0].type; - } - - function GetFormResult() { - if (logElementGetting) - Log("getting form result"); - var t = document.getElementsByTagName("form")[0].childNodes[0].childNodes; - if (t.length > 0 && t[0].tagName == undefined) // debreceni moodle - return document.getElementsByTagName("form")[1].childNodes[0].childNodes; - else - return t; - } - - function GetQText(i) { - if (logElementGetting) - Log("getting qtext by index: " + i); - var results = GetFormResult(); // getting results element - return results[i].getElementsByClassName("qtext"); - } - - function GetDropboxes(i) { - if (logElementGetting) - Log("getting dropboxes by index: " + i); - var results = GetFormResult(); // getting results element - return results[i].getElementsByTagName("select"); - } - - function GetCurrQuestion(i) { - if (logElementGetting) - Log("getting curr questions by index: " + i); - return document.getElementsByTagName("form")[0].childNodes[0].childNodes[i].childNodes[1].childNodes[ - 0].innerText; - } - - function GetCurrentAnswer(i) { - if (logElementGetting) - Log("getting curr answer by index: " + i); - var results = GetFormResult(); // getting results element - var t = results[i].getElementsByClassName("formulation clearfix")[0].getElementsByTagName("span"); - if (t.length > 2) - return t[1].innerHTML.split("
")[1]; - } - - function GetSelectAnswer() { - if (logElementGetting) - Log("getting selected answer"); - var t = document.getElementsByTagName("select"); - if (t.length > 0) - return t[0].options[document.getElementsByTagName("select")[ - 0].selectedIndex].innerText; - } - - function GetAnswerNode(i) { - if (logElementGetting) - Log("getting answer node"); - - var results = GetFormResult(); // getting results element - - var r = results[i].getElementsByClassName("answer")[0].childNodes; - var ret = []; - for (var j = 0; j < r.length; j++) - if (r[j].tagName != undefined && r[j].tagName.toLowerCase() == "div") - ret.push(r[j]); - - let qtype = DetermineQuestionType(ret); - - return { - nodes: ret, - type: qtype - }; - } - - function DetermineQuestionType(nodes) { - let qtype = ""; - let i = 0; - - while (i < nodes.length && qtype == "") { - let inps = nodes[i].getElementsByTagName("input"); - - if (inps.length > 0) { - qtype = inps[0].type; - } - - i++; - } - - return qtype; - - } - - function GetPossibleAnswers(i) { - if (logElementGetting) - Log("getting possible answers"); - var results = GetFormResult(); // getting results element - var items = GetFormResult()[i].getElementsByTagName("label"); - var r = []; - for (var j = 0; j < items.length; j++) { - - function TryGetCorrect(j) { - var cn = items[j].parentNode.className; - if (cn.includes("correct")) - return cn.includes("correct") && !cn.includes("incorrect"); - } - r.push({ - value: items[j].innerText, - iscorrect: TryGetCorrect(j) - }); - } - return r; - } - - function GetRightAnswerIfCorrectShown(i) { - if (logElementGetting) - Log("getting right answer if correct shown"); - var results = GetFormResult(); // getting results element - return results[i].getElementsByClassName("rightanswer"); - } - - function GetWrongAnswerIfCorrectNotShown(i) { - if (logElementGetting) - Log("getting wrong answer if correct not shown"); - var results = GetFormResult(); // getting results element - var n = results[i].getElementsByTagName("i")[0].parentNode; - if (n.className.includes("incorrect")) - return results[i].getElementsByTagName("i")[0].parentNode.innerText; - else - return ""; - } - - function GetRightAnswerIfCorrectNotShown(i) { - if (logElementGetting) - Log("Getting right answer if correct not shown"); - var results = GetFormResult(); // getting results element - var n = results[i].getElementsByTagName("i")[0].parentNode; - if (n.className.includes("correct") && !n.className.includes("incorrect")) - return results[i].getElementsByTagName("i")[0].parentNode.innerText; - else - return ""; - } - - function GetFormCFOfResult(result) { - if (logElementGetting) - Log("getting formulation clearfix"); - return result.getElementsByClassName("formulation clearfix")[0]; - } - - function GetResultText(i) { - if (logElementGetting) - Log("getting result text"); - var results = GetFormResult(); // getting results element - return GetFormCFOfResult(results[i]).getElementsByTagName("p"); - } - - function GetResultImage(i) { - if (logElementGetting) - Log("getting result image"); - var results = GetFormResult(); // getting results element - return GetFormCFOfResult(results[i]).getElementsByTagName("img"); - } - - // this function should return the question, posible answers, and image names - function GetQuestionFromTest() { - var questions; // the important questions - var allQuestions; // all questions - try // trying to get tha current questions - { - allQuestions = GetAllQuestionsQtext(); // getting questions - if (allQuestions.length == 0) // if there are no found questions - { - var ddq = GetAllQuestionsDropdown(); - if (EmptyOrWhiteSpace(ddq)) { - var questionData = ""; - for (var j = 0; j < allQuestions.length; j++) { - allQuestions = GetAllQuestionsQtext()[j].childNodes; - for (var i = 0; i < allQuestions.length; i++) { - if (allQuestions[i].data != undefined && !EmptyOrWhiteSpace(allQuestions[i].data)) // if the current element has some text data to add - { - questionData += allQuestions[i].data + " "; // adding text to question data - } - } - } - questions = [questionData]; - } else - questions = [ddq]; - } else // if there is - { - questions = []; - for (var i = 0; i < allQuestions.length; i++) { - questions.push(allQuestions[i].innerText); - } - } - } catch (e) { - Exception(e, "script error at getting question:"); - } - var imgNodes = ""; // the image nodes for questions - try { - imgNodes = GetQuestionImages(); // getting question images, if there is any - AddImageNamesToImages(imgNodes); // adding image names to images, so its easier to search for, or even guessing - } catch (e) { - Log(e); - Log("Some error with images"); - } - - questions = questions.map((item, ind) => { - return ReplaceCharsWithSpace(item, "\n"); - }); - - return { - imgnodes: imgNodes, - allQ: allQuestions, - q: questions - }; - } - - // gets the question from the result page - // i is the index of the question - function GetQuestionFromResult(i) { - var temp = GetQText(i); - var currQuestion = ""; - if (temp.length > 0) // if the qtext element is not 0 length - { - currQuestion = temp[0].innerText; // adding the question to curr question as .q - } else { - // this is black magic fuckery a bit - if (GetDropboxes(i).length > 0) // if there is dropdown list in the current question - { - var allNodes = GetResultText(i); - currQuestion = ""; - for (var k = 0; k < allNodes.length; k++) { - var allQuestions = GetResultText(i)[k].childNodes; - for (var j = 0; j < allQuestions.length; j++) { - if (allQuestions[j].data != undefined && !EmptyOrWhiteSpace(allQuestions[j].data)) { - currQuestion += allQuestions[j].data + " "; - } - } - } - } else { - try { - currQuestion = GetCurrQuestion(i); - } catch (e) { - currQuestion = "REEEEEEEEEEEEEEEEEEEEE"; // this shouldnt really happen sry guys - } - } - } - return currQuestion; - } - - // tries to get right answer from result page - // i is the index of the question - function GetRightAnswerFromResult(i) { - var fun = []; - - // the basic type of getting answers - fun.push(function TryGet0(i) { - var temp = GetRightAnswerIfCorrectShown(i); // getting risht answer - if (temp.length > 0) // if the rightanswer element is not 0 length - return temp[0].innerText; // adding the answer to curr question as .a - }); - - // if there is dropdown list in the current question - fun.push(function TryGet1(i) { - if (GetDropboxes(i).length > 0) - return GetCurrentAnswer(i); - }); - - // if the correct answers are not shown, and the selected answer - // is correct - fun.push(function TryGet2(i) { - return GetRightAnswerIfCorrectNotShown(i); - }); - - // if there is dropbox in the question - fun.push(function TryGet3(i) { - return GetSelectAnswer(); - }); - - // if the correct answers are not shown, and the selected answer - // is incorrect, and there are only 2 options - fun.push(function TryGet4(i) { - var possibleAnswers = GetPossibleAnswers(i); - if (possibleAnswers.length == 2) { - for (var k = 0; k < possibleAnswers.length; k++) - if (possibleAnswers[k].iscorrect == undefined) - return possibleAnswers[k].value; - } - }); - - fun.push(function TryGetFinal(i) { - return undefined; - }); - - var j = 0; - var currAnswer; - while (j < fun.length && EmptyOrWhiteSpace(currAnswer)) { - currAnswer = fun[j](i); - j++; - } - - return currAnswer; - } - - // version 2 of getting right answer from result page - // i is the index of the question - function GetRightAnswerFromResultv2(i) { - try { - var answerNodes = GetAnswerNode(i); - let items = answerNodes.nodes; - - if (answerNodes.type == "checkbox") - return GetRightAnswerFromResult(i); - - for (var j = 0; j < items.length; j++) { - var cn = items[j].className; - if (cn.includes("correct") && !cn.includes("incorrect")) - return items[j].innerText; - } - if (items.length == 2) { - for (var j = 0; j < items.length; j++) { - var cn = items[j].className; - if (!cn.includes("correct")) - return items[j].innerText; - } - } - } catch (e) { - Log("error at new nodegetting, trying the oldschool way"); - } - } - - //: }}} - //: Main logic stuff {{{ //: Loading {{{ @@ -817,10 +835,14 @@ //: }}} + var GetFileData = () => { + return GM_getResourceText("data"); + }; + function ReadFile(cwith) { var resource = ""; try { - resource = GM_getResourceText("data"); // getting data from txt + resource = GetFileData(); // getting data from txt if (resource == undefined) { ShowMessage({ @@ -947,7 +969,7 @@ try { currQuestion.I = JSON.parse(currData); } catch (e) { - currQuestion.I = [currData]; + currQuestion.I = currData.split(','); } } ExpectedIdentifier = ['?', '+']; @@ -1045,8 +1067,9 @@ while (i < data.length && !GM_getValue("Is" + i + "Active")) { i++; } - if (i >= data.length) - document.getElementById("HelperMenuButton").style.background = "yellow"; + + // TODO: move this + AlertOnNoQuestion(i); } catch (e) { Exception(e, "script error at loading:"); @@ -1055,6 +1078,15 @@ cwith(count, subjCount); } + var AlertOnNoQuestion = (i) => { + try { + if (i >= data.length) + document.getElementById("HelperMenuButton").style.background = "yellow"; + } catch (e) { + Log("Unable to get helper menu button"); + } + }; + //: }}} //: UI handling {{{ @@ -1065,7 +1097,9 @@ try // try, cus im suspicious { newVersion = GM_info.script.version !== GM_getValue("lastVerson"); - } catch (e) {} + } catch (e) { + Log("Some weird error trying to set new verison"); + } var greetMsg = ""; // message to show at the end var timeout = null; // the timeout. if null, it wont be hidden // no new version, nothing loaded @@ -1082,7 +1116,7 @@ greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". "; if (lastestVersion != undefined && GM_info.script.version != lastestVersion) { - greetMsg += "Új verzió elérhető: " + lastestVersion + " "; + greetMsg += "Új verzió elérhető: " + lastestVersion + "\n"; timeout = undefined; } greetMsg += count + " kérdés és " + subjCount + " tárgy betöltve. (click for help)."; @@ -1153,7 +1187,7 @@ //: Answering stuffs {{{ function HandleQuiz() { - var q = GetQuestionFromTest(); + var q = QPM.GetQuestionFromTest(); var questions = q.q; var allQuestions = q.allQ; var imgNodes = q.imgnodes; @@ -1204,7 +1238,7 @@ m: "Nincs találat :( Kattints az üzenetre az összes kérdés/válaszért manuális kereséshez! Előfordulhat, hogy a tárgyat nem válsztottad ki a menüben.", isSimple: true }, undefined, function() { - GM_openInTab(serverAdress + 'legacy', { + GM_openInTab(serverAdress + 'lred', { active: true }); }); @@ -1276,7 +1310,7 @@ function GetImageFormResult(i) { var temp = null; try { - var imgElements = GetResultImage(i); // trying to get image + var imgElements = RPM.GetResultImage(i); // trying to get image var imgURL = []; // image urls for (var j = 0; j < imgElements.length; j++) { if (!imgElements[j].src.includes("brokenfile")) // idk why brokenfile is in some urls, which are broken, so why tf are they there damn moodle @@ -1290,7 +1324,9 @@ temp = JSON.stringify(imgURL); return temp; } - } catch (e) {} + } catch (e) { + Log("Couldn't get images from result"); + } } // saves the current quiz. questionData contains the active subjects questions @@ -1328,7 +1364,7 @@ var sentData = {}; try { try { - sentData.subj = GetCurrentSubjectName(); + sentData.subj = MPM.GetCurrentSubjectName(); } catch (e) { sentData.subj = "NOSUBJ"; Log("unable to get subject name :c"); @@ -1355,33 +1391,35 @@ function GetQuiz() { try { var quiz = []; // final quiz stuff - var results = GetFormResult(); // getting results element + var results = RPM.GetFormResult(); // getting results element for (var i = 0; i < results.length - 2; i++) // going through results, except last two, idk why, dont remember, go check it man { var question = {}; // the current question // QUESTION -------------------------------------------------------------------------------------------------------------------- - var q = GetQuestionFromResult(i); + var q = RPM.GetQuestionFromResult(i); if (q != undefined) question.q = SimplifyQuery(q); // RIGHTANSWER --------------------------------------------------------------------------------------------------------------------- - var a = GetRightAnswerFromResultv2(i); + var a = RPM.GetRightAnswerFromResultv2(i); if (a == undefined) - a = GetRightAnswerFromResult(i); + a = RPM.GetRightAnswerFromResult(i); if (a != undefined) question.a = SimplifyQuery(a); // IMG --------------------------------------------------------------------------------------------------------------------- var img = GetImageFormResult(i); question.i = img; - q = ReplaceCharsWithSpace(q, "\n"); - a = ReplaceCharsWithSpace(a, "\n"); + if (q != undefined) + q = ReplaceCharsWithSpace(q, "\n"); + if (a != undefined) + a = ReplaceCharsWithSpace(a, "\n"); if (question.a != undefined) // adding only if has question { quiz.push(new Question(question.q, question.a, question.i)); // adding current question to quiz } else { - Log("error getting queston, or its incorrect"); + Log("error getting queston, no correct answer given, or its incorrect"); Log(question); } } @@ -1434,7 +1472,7 @@ document.addEventListener("keydown", function(e) // keydown event listener { try { - var video = GetVideo(); + var video = MPM.GetVideo(); var keyCode = e.keyCode; // getting keycode if (keyCode == 32) // if the keycode is 32 (space) { @@ -1458,7 +1496,7 @@ Log(err.message); } }); - var toadd = GetVideoElement(); + var toadd = MPM.GetVideoElement(); var node = CreateNodeWithText(toadd, "Miután elindítottad: Play/pause: space. Seek: Bal/jobb nyíl."); node.style.margin = "5px 5px 5px 5px"; // fancy margin @@ -1514,7 +1552,7 @@ try { if (results.length > 0) // if there are items in the result { - var answers = GetAllAnswer(currQuestionNumber); // getting all answers + var answers = RPM.GetAllAnswer(currQuestionNumber); // getting all answers var toColor = []; // the numberth in the array will be colored, and .length items will be colored var type = ""; // type of the question. radio or ticbox or whatitscalled for (var i = 0; i < answers.length; i++) // going thtough answers @@ -1533,12 +1571,12 @@ if (NormalizeSpaces(RemoveUnnecesarySpaces(correct)).includes(answer)) // if the correct answer includes the current answer { toColor.push(i); // adding the index - type = GetInputType(answers, i); // setting the type + type = MPM.GetInputType(answers, i); // setting the type } } } if (results[0].match == 100) // if the result is 100% correct - if (type !== "radio" || toColor.length == 1) // if the type is not radio or there is exactly one correct solution + if (type !== "radio" || toColor.length == 1) // TODO why not radio for (var i = 0; i < toColor.length; i++) // going through "toColor" answers[toColor[i]].style.backgroundColor = "#8cff66"; // and coloring the correct index } @@ -1619,6 +1657,7 @@ mainDiv.style.top = (startFromTop) + 'px'; mainDiv.style.left = (window.innerWidth - width) / 2 + 'px'; mainDiv.style.opacity = "0.9"; // setting starting opacity + mainDiv.setAttribute("id", "scriptMessage"); var matchPercent = msgItem[0][0].p; if (isSimpleMessage) { var simpleMessageParagrapg = document.createElement("p"); // new paragraph @@ -2176,6 +2215,10 @@ } function ReplaceCharsWithSpace(val, char) { + + assert(val); + assert(char); + toremove = NormalizeSpaces(val); var regex = new RegExp(char, "g"); @@ -2233,6 +2276,11 @@ }); } + var assert = (val) => { + if (!val) + throw new Error("Assertion failed"); + }; + //: }}} //: Help {{{