diff --git a/main.js b/main.js index 6f6d461..d296bb9 100644 --- a/main.js +++ b/main.js @@ -20,28 +20,26 @@ ------------------------------------------------------------------------- */ -// TODO: -// default is not active on new subjects -> TEST - var data; // all data, which is in the resource txt var addEventListener; // add event listener function const lastChangeLog = - '- Eredmények oldalon kérdésekre válasz szerzés mód váltás\n - Görgethető tárgyak, mert túl sok van már :p\nHa rosz választ szed ki a script pls küldj feedbacket! IRC is van.'; + '- Passzív mód: ha bepipálod a menü gomb alatt, akkor nem tölti be minden alkalommal a kérdéseket (csak csendben vár).\n - Pár lényeges bugfix\n - Ha találkoztok bugokkal, akkor pls report! thanx'; const serverAdress = "https://qmining.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 = false; +const forceTestPage = true; const forceResultPage = false; const forceDefaultPage = false; const logElementGetting = false; -const log = false; +const log = true; const motdShowCount = 3; var motd = ""; -var lastestVersion = ""; // TODO: if undefined no new verion +var lastestVersion = ""; -const minMatchAmmount = 49; +const minMatchAmmount = 55; +const lengthDiffMultiplier = 10; //: Class descriptions {{{ class Question { @@ -100,8 +98,6 @@ class Question { } } static CompareString(s1, s2) { - //if (s1 == undefined || s2 == undefined) - // return 0; s1 = SimplifyStringForComparison(s1).split(" "); s2 = SimplifyStringForComparison(s2).split(" "); var match = 0; @@ -110,7 +106,7 @@ class Question { match++; var percent = Math.round(((match / s1.length) * 100).toFixed(2)); // matched words percent var lengthDifference = Math.abs(s2.length - s1.length); - percent -= lengthDifference * 3; + percent -= lengthDifference * lengthDiffMultiplier; if (percent < 0) percent = 0; return percent; @@ -268,7 +264,14 @@ function Main() { function GetAllQuestionsDropdown() { if (logElementGetting) Log("getting dropdown question"); - return document.getElementById("responseform").getElementsByTagName("p")[0].innerText; + 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() { @@ -290,7 +293,6 @@ function GetFormulationClearfix() { } function GetAnswerOptions() { - // TODO if (logElementGetting) Log("getting all answer options"); return GetFormulationClearfix()[0].childNodes[3].innerText; @@ -531,6 +533,11 @@ function GetQuestionFromTest() { Log(e); Log("Some error with images"); } + + questions = questions.map((item, ind) => { + return ReplaceCharsWithSpace(item, "\n"); + }); + return { imgnodes: imgNodes, allQ: allQuestions, @@ -772,8 +779,13 @@ function ReadFile(cwith) { var resource = ""; try { resource = GM_getResourceText("data"); // getting data from txt - if (resource == null) { - return; // TODO show some msg what happened + if (resource == undefined) { + + ShowMessage({ + m: "Nem lehetett beolvasni a fájlt :c Ellenőrizd az elérési utat, vagy a fájl jogosultságokat", + isSimple: true + }); + return; } if (EmptyOrWhiteSpace(resource)) { throw { @@ -812,7 +824,6 @@ function ReadNetDB(cwith, useNetDB) { /* * Returns a question database from the given data. * Parameter should be raw read file in string with "\n"-s - * TODO: ??? -s are not listed as errors, tho works correctly * */ function ParseRawData(data) { @@ -910,6 +921,11 @@ function ParseRawData(data) { function Load(cwith) { var useNetDB = GM_getValue("useNetDB"); + let skipLoad = GM_getValue("skipLoad"); + + if (skipLoad) + return -1; + if (useNetDB != undefined && useNetDB == 1) return ReadNetDB(cwith, useNetDB); else @@ -944,7 +960,16 @@ function NLoad(resource, cwith) { d = JSON.parse(resource); } catch (e) { Log("Old data, trying with old methods...."); - d = ParseRawData(resource).result; + try { + d = ParseRawData(resource).result; + } catch (e2) { + Log("Couldt parse data!"); + ShowMessage({ + m: "Nem sikerült betölteni az adatokat! Ellenőriz a megadott fájlt, vagy az internetelérésed!", + isSimple: true + }); + return; + } } var r = new QuestionDB(); var rt = []; @@ -1003,7 +1028,7 @@ function HandleUI(url, count, subjCount) { 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!"; + "Hiba a @resource tagnál, vagy a fileval van gond! (Lehet át lett helyezve, vagy üres.) Vagy válaszd a netes adatok használatát menüben. 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 @@ -1098,7 +1123,6 @@ function HandleQuiz() { answers.push(r); HighLightAnswer(result, j); // highlights the answer for the current result } - console.log(GetAnswerOptions()); ShowAnswers(answers); } @@ -1156,7 +1180,7 @@ function ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentDa if (addedQ > 0) // if there are more than 0 new question { msg = "Klikk ide a nyers adatokhoz. " + addedQ + - " új kérdés! Ne felejtsd el bemásolni a fő txt-be!"; + " új kérdés!"; var useNetDB = GM_getValue("useNetDB"); if (useNetDB != undefined && useNetDB == 1) { @@ -1164,7 +1188,9 @@ function ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentDa msg += " Nem sikerült kérdéseket elküldeni szervernek. Ha gondolod utánanézhetsz."; else msg += "Az új kérdések elküldve."; - } + } else + msg += "Ne felejtsd el bemásolni a fő txt-be!"; + } else // if there is 0 or less new question { msg = "A kérdőívben nincsen új kérdés. Ha mégis le akarod menteni klikk ide."; @@ -1285,6 +1311,7 @@ function GetQuiz() { var q = GetQuestionFromResult(i); if (q != undefined) question.q = SimplifyQuery(q); + // RIGHTANSWER --------------------------------------------------------------------------------------------------------------------- var a = GetRightAnswerFromResultv2(i); if (a == undefined) @@ -1295,6 +1322,9 @@ function GetQuiz() { var img = GetImageFormResult(i); question.i = img; + q = ReplaceCharsWithSpace(q, "\n"); + 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 @@ -1707,8 +1737,8 @@ function ShowMessage(msgItem, timeout, funct) { // shows a fancy menu function ShowMenu() { try { - var buttonWidth = 75; // button size ;) - var buttonHeight = 55; + var buttonWidth = 100; // button size ;) + var buttonHeight = 85; var appedtTo = document.body; // will be appended here // mainDiv.style.left = (window.innerWidth - width) / 2 + 'px'; @@ -1721,20 +1751,28 @@ function ShowMenu() { menuButtonDiv.style.left = window.innerWidth - buttonWidth * 1.5 + 'px'; menuButtonDiv.style.zIndex = 999999; // TO THE MAX menuButtonDiv.style.position = "fixed"; + // menuButtonDiv.style.borderStyle = "solid"; + // menuButtonDiv.style.borderWidth = "1px"; // design menuButtonDiv.style.textAlign = "center"; menuButtonDiv.style.padding = "0px"; - menuButtonDiv.style.background = "#222d32"; // background color - menuButtonDiv.style.color = "#ffffff"; // text color - menuButtonDiv.style.borderColor = "#035a8f"; // border color - menuButtonDiv.style.border = "solid"; - menuButtonDiv.style.opacity = "0.9"; // setting starting opacity + menuButtonDiv.style.margin = "0px"; + menuButtonDiv.style.background = "transparent"; // background color // menu text - var menuTextBox = CreateNodeWithText(menuButtonDiv, "Kérdések\nMenü"); + // var menuTextBox = CreateNodeWithText(menuButtonDiv, "Kérdések\nMenü"); - menuButtonDiv.addEventListener("click", function() { + var menuButton = CreateNodeWithText(menuButtonDiv, "Kérdések Menu", "button"); + menuButton.style.width = buttonWidth + 'px'; + menuButton.style.border = 'none'; + menuButton.style.height = buttonHeight - 20 + 'px'; + menuButton.style.background = "#222d32"; // background color + menuButton.style.color = "#ffffff"; // background color + + + + menuButton.addEventListener("click", function() { if (document.getElementById("HelperMenu") == null) { ShowMenuList(); } else { @@ -1742,9 +1780,32 @@ function ShowMenu() { } }); // adding click - //addEventListener(window, 'scroll', function () { - // menuButtonDiv.style.top = (pageYOffset + window.innerHeight - buttonHeight * 2) + 'px'; - //}) + // passive mode stuff + var questionsTickBox = document.createElement("input"); + questionsTickBox.type = "checkbox"; + questionsTickBox.checked = GM_getValue("skipLoad"); + questionsTickBox.style.position = ""; + questionsTickBox.style.left = 10 + 'px'; + questionsTickBox.style.margin = "5px 5px 5px 5px"; // fancy margin + questionsTickBox.style.top = 0 + 'px'; + + menuButtonDiv.appendChild(questionsTickBox); // adding to main div + + questionsTickBox.addEventListener("click", function() { + GM_setValue("skipLoad", questionsTickBox.checked); + if (GM_getValue("skipLoad")) { + ShowMessage({ + m: "Passzív mód bekapcsolva, mostantól kérdések nem lesznek betöltve/lekérve.", + isSimple: true + }, 10); + } + + }); + var loadDataCheckBoxText = CreateNodeWithText(questionsTickBox, + "Passzív mód", "span"); + loadDataCheckBoxText.style.fontSize = "12px"; + + menuButtonDiv.appendChild(loadDataCheckBoxText); addEventListener(window, 'resize', function() { menuButtonDiv.style.left = window.innerWidth - buttonWidth * 2 + 'px'; @@ -1849,10 +1910,16 @@ function ShowMenuList() { { var noDataRow = tbl.insertRow(); var noDataRowCell = noDataRow.insertCell(); + var textBox; - var textBox = CreateNodeWithText(noDataRowCell, - "A kérdéseket nem lehetett beolvasni. Vagy nem elérhető a szerver, vagy ha offline módot használsz, akkor hibás a fájl elérési útja, vagy a fájl maga. Olvasd el a manualt!" - ); + if (GM_getValue("skipLoad")) + textBox = CreateNodeWithText(noDataRowCell, + "Passszív mód bekapcsolva. Kapcsold ki a kérdések betöltéséhez!" + ); + else + textBox = CreateNodeWithText(noDataRowCell, + "A kérdéseket nem lehetett beolvasni. Vagy nem elérhető a szerver, vagy ha offline módot használsz, akkor hibás a fájl elérési útja, vagy a fájl maga. Olvasd el a manualt!" + ); textBox.style.margin = fiveMargin; // fancy margin } @@ -1953,7 +2020,6 @@ function ShowMenuList() { var xButton = CreateNodeWithText(buttonCell, "Bezárás", "button"); xButton.style.position = ""; - xButton.style.background = "white"; xButton.style.left = 10 + 'px'; xButton.style.margin = "5px 5px 5px 5px"; // fancy margin xButton.style.top = menuDiv.offsetHeight + 'px'; @@ -1965,7 +2031,6 @@ function ShowMenuList() { var helpButton = CreateNodeWithText(buttonCell, "Help", "button"); helpButton.style.position = ""; - helpButton.style.background = "white"; helpButton.style.left = 10 + 'px'; helpButton.style.margin = "5px 5px 5px 5px"; // fancy margin helpButton.style.top = menuDiv.offsetHeight + 'px'; @@ -1997,7 +2062,7 @@ function ShowMenuList() { Exception(e, "script error at showing menu list:"); } - document.addEventListener("keydown", EscClose); + document.addEventListener("keydown", EscClose); } function EscClose(e) { @@ -2054,6 +2119,15 @@ function CreateNodeWithText(to, text, type) { return paragraphElement; } +function ReplaceCharsWithSpace(val, char) { + toremove = NormalizeSpaces(val); + + var regex = new RegExp(char, "g"); + toremove.replace(regex, " "); + + return RemoveUnnecesarySpaces(toremove); +} + // removes whitespace from begining and and, and replaces multiple spaces with one space function RemoveUnnecesarySpaces(toremove) { toremove = NormalizeSpaces(toremove);