From 6143cecc30a913d6e62b0348399234c2bf846344 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer <3167982-YourFriendlyNeighborhoodDealer@users.noreply.gitlab.com> Date: Fri, 14 Dec 2018 10:14:10 +0100 Subject: [PATCH] Results parsing improvements, and code folding, rearranging --- main.js | 404 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 205 insertions(+), 199 deletions(-) diff --git a/main.js b/main.js index bb6e8a0..a331806 100644 --- a/main.js +++ b/main.js @@ -1,3 +1,4 @@ +// vim:foldmethod=marker /* ---------------------------------------------------------------------------- Online Moodle/Elearning/KMOOC test help @@ -21,13 +22,13 @@ var data; // all data, which is in the resource txt var addEventListener; // add event listener function -var lastChangeLog = '- Csak egy plusz weboldal gomb a menüben.'; +var lastChangeLog = '- Csak egy plusz weboldal gomb a menüben.'; // TODO var serverAdress = "http://questionmining.tk/"; // forcing pages for testing. unless you test, do not set these to true! // only one of these should be true for testing -const forceTestPage = true; // TODO -const forceResultPage = false; +const forceTestPage = false; +const forceResultPage = true; const forceDefaultPage = false; const logElementGetting = false; @@ -35,10 +36,8 @@ var motdShowCount = 3; var motd = ""; var minMatchAmmount = 50; -// --------------------------------------------------------------------------------------------- -// Class descriptions -// --------------------------------------------------------------------------------------------- +//: Class descriptions {{{ class Question { constructor(q, a, i) { this.Q = q; @@ -220,10 +219,9 @@ class QuestionDB { return r.join("\n\n"); } } +//: }}} -// --------------------------------------------------------------------------------------------- -// Main function -// --------------------------------------------------------------------------------------------- +//: Main function {{{ function Main() { 'use strict'; console.clear(); @@ -257,11 +255,11 @@ function Main() { ); }); } -// --------------------------------------------------------------------------------------------- -// DOM getting stuff +//: }}} + +//: DOM getting stuff {{{ // all dom getting stuff are in this sections, so on // moodle dom change, stuff breaks here -// --------------------------------------------------------------------------------------------- function GetAllQuestionsDropdown() { if (logElementGetting) @@ -287,12 +285,25 @@ function GetFormulationClearfix() { return document.getElementsByClassName("formulation clearfix"); } +function GetAnswerOptions() { + // TODO + if (logElementGetting) + console.log("getting all answer options"); + return GetFormulationClearfix()[0].childNodes[3].innerText; +} + function GetQuestionImages() { if (logElementGetting) console.log("getting question images"); return GetFormulationClearfix()[0].getElementsByTagName("img"); } +function GetCurrentSubjectName() { + if (logElementGetting) + console.log("getting current subjects name"); + return document.getElementById("page-header").innerText.split("\n")[0]; +} + function GetVideo() { if (logElementGetting) console.log("getting video stuff"); @@ -610,10 +621,12 @@ function GetRightAnswerFromResultv2(i) { } } +//: }}} + +//: Main logic stuff {{{ + +//: Loading {{{ -//----------------------------------------------------------------------------------------------------- -// Interface stuff -//----------------------------------------------------------------------------------------------------- function Init(cwith) { if (false) // reset, only for testing! { @@ -677,160 +690,6 @@ function Init(cwith) { return count; } -function PrepareAnswers(result, j) { - if (result.length > 0) // if there are more than zero results - { - var allMessages = []; // preparing all messages - for (var k = 0; k < result.length; k++) // going throuh all results - { - var msg = ""; // the current message - if ((GM_getValue("showQuestions") == undefined) || GM_getValue("showQuestions")) // if the question should be shown - { - msg += result[k].q.Q + "\n"; // adding the question if yes - } - msg += result[k].q.A.replace(/, /g, "\n"); // adding answer - if (result[k].q.HasImage()) // and adding image, if it exists - { - msg += "\n" + result[k].q.I; // if it has image part, adding that too - } - allMessages.push({ - m: msg, - p: result[k].match - }); - } - return allMessages; - } -} - -function ShowAnswers(answers) { - if (answers.length > 0) { // if there are more than 0 answer - ShowMessage(answers); - } else { - ShowMessage({ - m: "Nincs találat :( TXT-ben szerepelhet a képnév/kérdés részlet, ha rákeresel! Illetve előfordulhat, hogy a tárgyat nem válsztottad ki a menüben.", - isSimple: true - }); - } -} - -function HandleQuiz(url, count) { - var q = GetQuestionFromTest(); - var questions = q.q; - var allQuestions = q.allQ; - var imgNodes = q.imgnodes; - // ------------------------------------------------------------------------------------------------------ - var answers = []; - for (var j = 0; j < questions.length; j++) // going thru all answers - { - var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question - var result = data.Search(question, SimplifyImages(imgNodes)); - answers.push(PrepareAnswers(result, j)); - HighLightAnswer(result, j); // highlights the answer for the current result - } - ShowAnswers(answers); -} - -function HandleUI(url, count) { - var newVersion = false; // if the script is newer than last start - var loaded = count != -1; // if script could load stuff - - try // try, cus im suspicious - { - newVersion = GM_info.script.version !== GM_getValue("lastVerson"); - } catch (e) {} - var greetMsg = ""; // message to show at the end - var timeout = null; // the timeout. if null, it wont be hidden - // no new version, nothing loaded - if (!newVersion && !loaded) // -------------------------------------------------------------------------------------------------------------- - { - greetMsg = - "Hiba a @resource tagnál, vagy a fileval van gond! (Lehet át lett helyezve, vagy üres.) Ellenőrizd az elérési utat, vagy hogy a Tampermonkey bővítmény eléri-e a fájlokat. Ha netes forrást használsz, akkor nem elérhető a szerver! Segítségért kattints!"; - } - var showSplash = (GM_getValue("showSplash") == undefined) || GM_getValue("showSplash"); // getting value, if splash screen should be shown. Its true, if its undefined, or true - // no new version, everything loaded, and show splash is enabled. otherwise something happened, so showing it - if (!newVersion && loaded && showSplash) // ------------------------------------------------------------------------------------------------ - { - greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". " + count + - " kérdés és " + data.length + " tárgy betöltve. (click for help)."; - console.log(data); - if (data.length > 0) { - var toAdd = []; - for (var i = 0; i < data.length; i++) { - if (data.GetIfActive(i)) { - toAdd.push(data.Subjects[i].Name + " (" + data.Subjects[i].length + ")"); - } - } - if (toAdd.length != 0) { - greetMsg += "\nAktív tárgyak: " + toAdd.join(", ") + "."; - } else { - greetMsg += "\nNincs aktív tárgyad. Menüből válassz ki eggyet!"; - } - } else { - greetMsg += " Az adatfájlban nem adtál meg nevet. Vagy nem elérhető a szerver. Katt a helpért!"; - // TODO if online mode print another error! - } - timeout = 5; - } - // new version, nothing loaded - if (newVersion && !loaded) // -------------------------------------------------------------------------------------------------------------- - { - greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + - ". Új verzió!\n Írd át a @resouce tagnál az elírési utat! Kivéve ha üres a file, akkor töltsd fel :) Nincs kérdés betöltve! Segítséghez kattints. Changelog:\n" + - lastChangeLog; // showing changelog too - } - // new version, everything loaded -> set lastVerson to current - if (newVersion && loaded) // -------------------------------------------------------------------------------------------------------------- - { - greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". " + count + - " kérdés és " + data.length + " tárgy betöltve. Verzió frissítve " + GM_info.script.version + - "-re. Changelog:\n" + lastChangeLog; - GM_setValue("lastVerson", GM_info.script.version); // setting lastVersion - } - console.log("MOTD " + motd); - if (!EmptyOrWhiteSpace(motd)) { - - var prevmotd = GM_getValue("motd"); - if (prevmotd != motd) { - greetMsg += "\nMOTD:\n" + motd; - timeout = null; - GM_setValue("motdcount", motdShowCount); - GM_setValue("motd", motd); - } else { - var motdcount = GM_getValue("motdcount"); - if (motdcount == undefined) { - GM_setValue("motdcount", motdShowCount); - motdcount = motdShowCount; - } - - motdcount--; - if (motdcount > 0) { - greetMsg += "\nMOTD:\n" + motd; - timeout = null; - GM_setValue("motdcount", motdcount); - } - - } - } - ShowMessage({ - m: greetMsg, - isSimple: true - }, timeout, ShowHelp); // showing message. If "m" is empty it wont show it, thats how showSplash works. -} - -function HandleResults(url) { - var allResults = []; - // TODO fix this - if (data) - for (var i = 0; i < data.length; i++) // going trough all subjects - if (data.GetIfActive(i)) // if the subject is active - allResults = allResults.concat(data[i].questions); // searching for the answer, with the given question and image (if there is) - SaveQuiz(GetQuiz(), allResults); // saves the quiz questions and answers -} - -//----------------------------------------------------------------------------------------------------- -// Main logic stuff -//----------------------------------------------------------------------------------------------------- - function ReadFile(cwith) { var resource = ""; try { @@ -942,6 +801,164 @@ function NLoad(resource, cwith) { cwith(count); } +//: }}} + +//: Answering stuffs {{{ + +function HandleQuiz(url, count) { + var q = GetQuestionFromTest(); + var questions = q.q; + var allQuestions = q.allQ; + var imgNodes = q.imgnodes; + // ------------------------------------------------------------------------------------------------------ + var answers = []; + for (var j = 0; j < questions.length; j++) // going thru all answers + { + var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question + var result = data.Search(question, SimplifyImages(imgNodes)); + answers.push(PrepareAnswers(result, j)); + HighLightAnswer(result, j); // highlights the answer for the current result + } + ShowAnswers(answers); +} + +function PrepareAnswers(result, j) { + if (result.length > 0) // if there are more than zero results + { + var allMessages = []; // preparing all messages + for (var k = 0; k < result.length; k++) // going throuh all results + { + var msg = ""; // the current message + if ((GM_getValue("showQuestions") == undefined) || GM_getValue("showQuestions")) // if the question should be shown + { + msg += result[k].q.Q + "\n"; // adding the question if yes + } + msg += result[k].q.A.replace(/, /g, "\n"); // adding answer + if (result[k].q.HasImage()) // and adding image, if it exists + { + msg += "\n" + result[k].q.I; // if it has image part, adding that too + } + allMessages.push({ + m: msg, + p: result[k].match + }); + } + return allMessages; + } +} + +function ShowAnswers(answers) { + if (answers.length > 0) { // if there are more than 0 answer + ShowMessage(answers); + } else { + ShowMessage({ + m: "Nincs találat :( TXT-ben szerepelhet a képnév/kérdés részlet, ha rákeresel! Illetve előfordulhat, hogy a tárgyat nem válsztottad ki a menüben.", + isSimple: true + }); + } +} + +function HandleUI(url, count) { + var newVersion = false; // if the script is newer than last start + var loaded = count != -1; // if script could load stuff + + try // try, cus im suspicious + { + newVersion = GM_info.script.version !== GM_getValue("lastVerson"); + } catch (e) {} + var greetMsg = ""; // message to show at the end + var timeout = null; // the timeout. if null, it wont be hidden + // no new version, nothing loaded + if (!newVersion && !loaded) // -------------------------------------------------------------------------------------------------------------- + { + greetMsg = + "Hiba a @resource tagnál, vagy a fileval van gond! (Lehet át lett helyezve, vagy üres.) Ellenőrizd az elérési utat, vagy hogy a Tampermonkey bővítmény eléri-e a fájlokat. Ha netes forrást használsz, akkor nem elérhető a szerver! Segítségért kattints!"; + } + var showSplash = (GM_getValue("showSplash") == undefined) || GM_getValue("showSplash"); // getting value, if splash screen should be shown. Its true, if its undefined, or true + // no new version, everything loaded, and show splash is enabled. otherwise something happened, so showing it + if (!newVersion && loaded && showSplash) // ------------------------------------------------------------------------------------------------ + { + greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". " + count + + " kérdés és " + data.length + " tárgy betöltve. (click for help)."; + console.log(data); + if (data.length > 0) { + var toAdd = []; + for (var i = 0; i < data.length; i++) { + if (data.GetIfActive(i)) { + toAdd.push(data.Subjects[i].Name + " (" + data.Subjects[i].length + ")"); + } + } + if (toAdd.length != 0) { + greetMsg += "\nAktív tárgyak: " + toAdd.join(", ") + "."; + } else { + greetMsg += "\nNincs aktív tárgyad. Menüből válassz ki eggyet!"; + } + } else { + greetMsg += " Az adatfájlban nem adtál meg nevet. Vagy nem elérhető a szerver. Katt a helpért!"; + } + timeout = 5; + } + // new version, nothing loaded + if (newVersion && !loaded) // -------------------------------------------------------------------------------------------------------------- + { + greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + + ". Új verzió!\n Írd át a @resouce tagnál az elírési utat! Kivéve ha üres a file, akkor töltsd fel :) Nincs kérdés betöltve! Segítséghez kattints. Changelog:\n" + + lastChangeLog; // showing changelog too + } + // new version, everything loaded -> set lastVerson to current + if (newVersion && loaded) // -------------------------------------------------------------------------------------------------------------- + { + greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". " + count + + " kérdés és " + data.length + " tárgy betöltve. Verzió frissítve " + GM_info.script.version + + "-re. Changelog:\n" + lastChangeLog; + GM_setValue("lastVerson", GM_info.script.version); // setting lastVersion + } + console.log("MOTD " + motd); + if (!EmptyOrWhiteSpace(motd)) { + + var prevmotd = GM_getValue("motd"); + if (prevmotd != motd) { + greetMsg += "\nMOTD:\n" + motd; + timeout = null; + GM_setValue("motdcount", motdShowCount); + GM_setValue("motd", motd); + } else { + var motdcount = GM_getValue("motdcount"); + if (motdcount == undefined) { + GM_setValue("motdcount", motdShowCount); + motdcount = motdShowCount; + } + + motdcount--; + if (motdcount > 0) { + greetMsg += "\nMOTD:\n" + motd; + timeout = null; + GM_setValue("motdcount", motdcount); + } + + } + } + ShowMessage({ + m: greetMsg, + isSimple: true + }, timeout, ShowHelp); // showing message. If "m" is empty it wont show it, thats how showSplash works. +} + +//: }}} + +//: Quiz saving {{{ + +function HandleResults(url) { + var allResults = new QuestionDB(); + // TODO fix this + if (data) + for (var i = 0; i < data.length; i++) // going trough all subjects + if (data.GetIfActive(i)) // if the subject is active + for (var j = 0; j < data.Subjects[i].Questions.length; j++) + allResults.AddQuestion("tmp", data.Subjects[i].Questions[j]); + SaveQuiz(GetQuiz(), allResults); // saves the quiz questions and answers +} + function ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentData) { var msg = ""; if (addedQ > 0) // if there are more than 0 new question @@ -971,22 +988,8 @@ function ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentDa } function SearchSameQuestion(questionData, quiz, i) { - var j = 0; - for (var j = 0; j < questionData.length; j++) { - var q1 = SimplifyStringForComparison(questionData[j].q); // existing data-s question - var q2 = SimplifyStringForComparison(quiz[i].q); // new datas question - var a1 = SimplifyStringForComparison(questionData[j].a); // existing data-s answer - var a2 = SimplifyStringForComparison(quiz[i].a); // new datas answer - if (q1.includes(q2) && a2.includes(a2)) { // if the questions and the answers match - if (quiz[i].i && questionData[j].i) { // if it has image questionData - if (questionData[j].i.includes(quiz[i].i)) // if they are the same - return j; - } else { - return j; - } - } - } - return -1; + var r = questionData.Search(quiz[i]); + return r.length; } // this should get the image url from a result page @@ -1046,7 +1049,7 @@ function SaveQuiz(quiz, questionData) { try { var subj = "NOSUBJ"; try { - subj = document.getElementById("page-header").innerText.split("\n")[0]; + subj = GetCurrentSubjectName(); } catch (e) {} var useNetDB = GM_getValue("useNetDB"); if (useNetDB != undefined && (useNetDB == 0 || useNetDB == 1)) { @@ -1102,7 +1105,7 @@ function GetQuiz() { if (question.a != undefined) // adding only if has question { - quiz.push(question); // adding current question to quiz + quiz.push(new Question(question.q, question.a, question.i)); // adding current question to quiz } else { console.log("error getting queston, or its incorrect"); console.log(question); @@ -1120,6 +1123,7 @@ function GetQuiz() { } +//: }}} function SimplifyImages(imgs) { var questionImages = []; // the array for the image names in question @@ -1135,9 +1139,9 @@ function SimplifyImages(imgs) { return questionImages; } -//----------------------------------------------------------------------------------------------------- -// Minor logic stuff -//----------------------------------------------------------------------------------------------------- +//: }}} + +//: Minor logic stuff {{{ // adds image names to image nodes function AddImageNamesToImages(imgs) { @@ -1266,9 +1270,9 @@ function HighLightAnswer(results, currQuestionNumber) { } } -//----------------------------------------------------------------------------------------------------- -// Minor UI stuff -//----------------------------------------------------------------------------------------------------- +//: }}} + +//: Minor UI stuff {{{ // shows a message with "msg" text, "matchPercent" tip and transp, and "timeout" time function ShowMessage(msgItem, timeout, funct) { @@ -1776,9 +1780,9 @@ function ShowMenuList() { } } -//----------------------------------------------------------------------------------------------------- -// Generic Helper stuff -//----------------------------------------------------------------------------------------------------- +//: }}} + +//: Generic utils {{{ function RemoveMultipleItems(array) { var newArray = []; @@ -1870,9 +1874,9 @@ function SendXHRMessage(message) { }); } -//----------------------------------------------------------------------------------------------------- -// Help -//----------------------------------------------------------------------------------------------------- +//: }}} + +//: Help {{{ // shows some neat help function ShowHelp() { @@ -1881,6 +1885,8 @@ function ShowHelp() { }); } +//: }}} +// // I am not too proud to cry that He and he // Will never never go out of my mind. // All his bones crying, and poor in all but pain,