Bug fixes, an performance improvements

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-17 17:08:13 +01:00
parent 3fcd18670e
commit 51ad5d7d4f

84
main.js
View file

@ -21,13 +21,13 @@
------------------------------------------------------------------------- */
// TODO:
// default is not active on new subjects
// send version, and check if new is avaible
// default is not active on new subjects -> TEST
// dropdown questions result gettin
var data; // all data, which is in the resource txt
var addEventListener; // add event listener function
var lastChangeLog =
' - Összes tárgy letiltása alapból, mert sok tárgy van, és belassulhat :c\n- Az azért jó, hogy sok tárgy van c:';
'- Performance javítások\n - Összes tárgy letiltása alapból, mert sok tárgy van, és belassulhat :c\n- Az azért jó, hogy sok tárgy van c:\nEllenőrizd, hogy mely tárgyak aktívak a menüben!';
var serverAdress = "https://questionmining.tk/";
// forcing pages for testing. unless you test, do not set these to true!
@ -39,8 +39,9 @@ const logElementGetting = false;
var motdShowCount = 3;
var motd = "";
var lastestVersion = "";
var minMatchAmmount = 50;
var minMatchAmmount = 49;
//: Class descriptions {{{
class Question {
@ -229,21 +230,25 @@ class QuestionDB {
//: Main function {{{
function Main() {
'use strict';
Init(function(count) {
Init(function(count, subjCount) {
var url = location.href;
try {
if ((url.includes("/quiz/") && url.includes("attempt.php")) || forceTestPage) { // if the current page is a test
HandleQuiz(url, count);
HandleQuiz();
} else if ((url.includes("/quiz/") && url.includes("review.php")) || forceResultPage) { // if the current window is a test-s result
HandleResults(url);
} else if ((!url.includes("/quiz/") && !url.includes("review.php") && !url.includes(".pdf")) ||
(forceDefaultPage)) { // if the current window is any other window than a quiz or pdf.
HandleUI(url, count);
HandleUI(url, count, subjCount);
}
} catch (e) {
ShowMessage({
m: "Fatál error. Check console (f12). Ha élesbe megy, akkor ctrl-f a kérdések.txt-ben",
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', {
active: true
});
});
console.log("------------------------------------------");
console.log("script error at main:");
@ -877,10 +882,19 @@ function LoadMOTD(resource) {
}
}
function LoadVersion(resource) {
try {
lastestVersion = resource.version;
} catch (e) {
console.log("Error loading version :c");
console.log(e);
}
}
// loading stuff
function NLoad(resource, cwith) {
var count = -1;
var subjCount = 0;
try {
var d = {};
try {
@ -893,9 +907,11 @@ function NLoad(resource, cwith) {
var rt = [];
var allCount = -1;
LoadMOTD(d);
LoadVersion(d);
for (var i = 0; i < d.Subjects.length; i++) {
let s = new Subject(d.Subjects[i].Name);
if (GM_getValue("Is" + i + "Active")) {
var j = 0;
for (j = 0; j < d.Subjects[i].Questions.length; j++) {
var currQ = d.Subjects[i].Questions[j];
@ -906,6 +922,8 @@ function NLoad(resource, cwith) {
count: j
});
allCount += j;
subjCount++;
}
r.AddSubject(s);
}
data = r;
@ -926,14 +944,14 @@ function NLoad(resource, cwith) {
if (i >= data.length)
document.getElementById("HelperMenuButton").style.background = "yellow";
cwith(count);
cwith(count, subjCount);
}
//: }}}
//: Answering stuffs {{{
function HandleQuiz(url, count) {
function HandleQuiz() {
var q = GetQuestionFromTest();
var questions = q.q;
var allQuestions = q.allQ;
@ -944,7 +962,9 @@ function HandleQuiz(url, count) {
{
var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question
var result = data.Search(question, SimplifyImages(imgNodes));
answers.push(PrepareAnswers(result, j));
var r = PrepareAnswers(result, j);
if (r != undefined)
answers.push(r);
HighLightAnswer(result, j); // highlights the answer for the current result
}
ShowAnswers(answers);
@ -980,13 +1000,17 @@ function ShowAnswers(answers) {
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.",
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', {
active: true
});
});
}
}
function HandleUI(url, count) {
function HandleUI(url, count, subjCount) {
var newVersion = false; // if the script is newer than last start
var loaded = count != -1; // if script could load stuff
@ -1007,8 +1031,13 @@ function HandleUI(url, count) {
if (!newVersion && loaded && showSplash) // ------------------------------------------------------------------------------------------------
{
timeout = 5;
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).";
greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". ";
if (GM_info.script.version != lastestVersion) {
greetMsg += "Új verzió elérhető: " + lastestVersion + " ";
timeout = undefined;
}
greetMsg += count + " kérdés és " + subjCount + " tárgy betöltve. (click for help).";
if (data.length > 0) {
var toAdd = [];
for (var i = 0; i < data.length; i++) {
@ -1037,7 +1066,7 @@ function HandleUI(url, count) {
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 +
" kérdés és " + subjCount + " tárgy betöltve. Verzió frissítve " + GM_info.script.version +
"-re. Changelog:\n" + lastChangeLog;
GM_setValue("lastVerson", GM_info.script.version); // setting lastVersion
}
@ -1076,12 +1105,7 @@ function HandleUI(url, count) {
function HandleResults(url) {
var allResults = new QuestionDB();
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
SaveQuiz(GetQuiz(), data); // saves the quiz questions and answers
}
function ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentData) {
@ -1540,7 +1564,6 @@ function ShowMessage(msgItem, timeout, funct) {
function SetQuestionText() {
var relevantQuestion = GetRelevantQuestion();
console.log("setting shit");
questionTextElement.innerText = relevantQuestion.m;
if (currItem == 0 && currRelevantQuestion == 0) {
numberTextBox.innerText = (currRelevantQuestion + 1) + ".";
@ -1708,7 +1731,7 @@ function ShowMenuList() {
menuDiv.style.color = "#ffffff"; // text color
menuDiv.style.borderColor = "#035a8f"; // border color
menuDiv.style.border = "solid";
menuDiv.style.opacity = "0.9"; // setting starting opacity
menuDiv.style.opacity = "1"; // setting starting opacity
var fiveMargin = "5px 5px 5px 5px";
var tbl = document.createElement('table');
@ -1720,7 +1743,7 @@ function ShowMenuList() {
var tr = tbl.insertRow();
var header1 = tr.insertCell();
var headerSubjInfoParagraph = CreateNodeWithText(header1, "Tárgynév (darab kérdés)", "center");
var headerSubjInfoParagraph = CreateNodeWithText(header1, "Tárgynév [darab kérdés]", "center");
headerSubjInfoParagraph.style.margin = fiveMargin; // fancy margin
var header2 = tr.insertCell();
@ -1733,15 +1756,20 @@ function ShowMenuList() {
subjRow.style.border = "1px solid #131319";
var td = subjRow.insertCell();
var textBox = CreateNodeWithText(td, data.Subjects[i].Name + " (" + data.Subjects[i].length +
")");
var text = data.Subjects[i].Name;
if (data.Subjects[i].length != 0)
text += " [ " + data.Subjects[i].length + "db ]";
var textBox = CreateNodeWithText(td, text);
textBox.style.margin = fiveMargin; // fancy margin
td = subjRow.insertCell();
var checkbox = document.createElement("input"); // new paragraph
checkbox.type = "checkbox";
checkbox.style.background = "white";
checkbox.style.margin = "5px 5px 5px 5px"; // fancy margin
checkbox.style.margin =
"5px 5px 5px 5px"; // fancy margin
td.appendChild(checkbox); // adding text box to main td
var active = data.GetIfActive(i);