Small code refactors

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-20 10:00:16 +01:00
parent f9244c9911
commit 646815ad97

242
main.js
View file

@ -22,7 +22,6 @@
// TODO: // TODO:
// default is not active on new subjects -> TEST // default is not active on new subjects -> TEST
// dropdown questions result gettin
var data; // all data, which is in the resource txt var data; // all data, which is in the resource txt
var addEventListener; // add event listener function var addEventListener; // add event listener function
@ -32,11 +31,11 @@ var serverAdress = "https://questionmining.tk/";
// forcing pages for testing. unless you test, do not set these to true! // forcing pages for testing. unless you test, do not set these to true!
// only one of these should be true for testing // only one of these should be true for testing
const forceTestPage = false; const forceTestPage = true; // TODO: set to false
const forceResultPage = false; const forceResultPage = false;
const forceDefaultPage = false; const forceDefaultPage = false;
const logElementGetting = false; const logElementGetting = false;
const log = false; const log = true; // TODO: this is false
var motdShowCount = 3; var motdShowCount = 3;
var motd = ""; var motd = "";
@ -231,6 +230,7 @@ class QuestionDB {
//: Main function {{{ //: Main function {{{
function Main() { function Main() {
'use strict'; 'use strict';
console.clear(); // TODO: remove dis
Init(function(count, subjCount) { Init(function(count, subjCount) {
var url = location.href; var url = location.href;
try { try {
@ -251,12 +251,7 @@ function Main() {
active: true active: true
}); });
}); });
Log("------------------------------------------"); Exception(e, "script error at main:");
Log("script error at main:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
if (url.includes("eduplayer")) // if the current site is a video site if (url.includes("eduplayer")) // if the current site is a video site
AddVideoHotkeys(url); // adding video hotkeys AddVideoHotkeys(url); // adding video hotkeys
@ -501,12 +496,7 @@ function GetQuestionFromTest() {
} }
} }
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at getting question:");
Log("script error at getting question:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
var imgNodes = ""; // the image nodes for questions var imgNodes = ""; // the image nodes for questions
try { try {
@ -547,7 +537,7 @@ function GetQuestionFromResult(i) {
} }
} else { } else {
try { try {
currQuestion = GetCurrSubj(i); currQuestion = GetCurrQuestion(i);
} catch (e) { } catch (e) {
currQuestion = "REEEEEEEEEEEEEEEEEEEEE"; // this shouldnt really happen sry guys currQuestion = "REEEEEEEEEEEEEEEEEEEEE"; // this shouldnt really happen sry guys
} }
@ -665,19 +655,12 @@ function Init(cwith) {
} }
}()); }());
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at addEventListener:");
Log("script error at addEventListener:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
VersionActions(); VersionActions();
count = Load(cwith); // loads resources count = Load(cwith); // loads resources
if (!url.includes(".pdf")) // showing menu unless its a .pdf page if (!url.includes(".pdf"))
{
ShowMenu(); ShowMenu();
}
return count; return count;
} }
@ -729,12 +712,7 @@ function ReadFile(cwith) {
}; };
} }
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at reading file:");
Log("script error at reading file:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
NLoad(resource, cwith); NLoad(resource, cwith);
} }
@ -758,12 +736,7 @@ function ReadNetDB(cwith, useNetDB) {
Log("Sending XMLHTTP Request..."); Log("Sending XMLHTTP Request...");
return NewXMLHttpRequest(); return NewXMLHttpRequest();
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at reading online database:");
Log("script error at reading online database:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
} }
@ -929,88 +902,24 @@ function NLoad(resource, cwith) {
} }
data = r; data = r;
count = allCount + 1; // couse starting with -1 to show errors count = allCount + 1; // couse starting with -1 to show errors
var i = 0;
while (i < data.length && !GM_getValue("Is" + i + "Active")) {
i++;
}
if (i >= data.length)
document.getElementById("HelperMenuButton").style.background = "yellow";
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at loading:");
Log("script error at loading:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
count = -1; // returns -1 if error count = -1; // returns -1 if error
} }
var i = 0;
while (i < data.length && !GM_getValue("Is" + i + "Active")) {
i++;
}
if (i >= data.length)
document.getElementById("HelperMenuButton").style.background = "yellow";
cwith(count, subjCount); cwith(count, subjCount);
} }
//: }}} //: }}}
//: Answering stuffs {{{ //: UI handling {{{
function HandleQuiz() {
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));
var r = PrepareAnswers(result, j);
if (r != undefined)
answers.push(r);
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 :( 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, subjCount) { function HandleUI(url, count, subjCount) {
var newVersion = false; // if the script is newer than last start var newVersion = false; // if the script is newer than last start
var loaded = count != -1; // if script could load stuff var loaded = count != -1; // if script could load stuff
@ -1102,6 +1011,70 @@ function HandleUI(url, count, subjCount) {
//: }}} //: }}}
//: Answering stuffs {{{
function HandleQuiz() {
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));
var r = PrepareAnswers(result, j);
if (r != undefined)
answers.push(r);
HighLightAnswer(result, j); // highlights the answer for the current result
}
console.log(GetAnswerOptions());
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 :( 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
});
});
}
}
//: }}}
//: Quiz saving {{{ //: Quiz saving {{{
function HandleResults(url) { function HandleResults(url) {
@ -1223,17 +1196,11 @@ function SaveQuiz(quiz, questionData) {
sendSuccess = true; sendSuccess = true;
} }
} catch (e) { } catch (e) {
Log("error at sending data to server."); Exception(e, "error at sending data to server.");
Log(e);
} }
ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentData); ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentData);
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at saving quiz");
Log("script error at saving quiz:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
} }
@ -1270,12 +1237,7 @@ function GetQuiz() {
} }
return quiz; return quiz;
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at quiz parsing:");
Log("script error at quiz parsing:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
} }
@ -1431,6 +1393,15 @@ function Log(value) {
console.log(value); console.log(value);
} }
function Error(e, msg){
Log("------------------------------------------");
Log(msg);
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
}
//: }}} //: }}}
//: Minor UI stuff {{{ //: Minor UI stuff {{{
@ -1649,12 +1620,7 @@ function ShowMessage(msgItem, timeout, funct) {
} }
}); });
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at showing message:");
Log("script error at showing message:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
} }
@ -1707,12 +1673,7 @@ function ShowMenu() {
appedtTo.appendChild(menuButtonDiv); appedtTo.appendChild(menuButtonDiv);
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at showing menu:");
Log("script error at showing menu:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
} }
@ -1935,12 +1896,7 @@ function ShowMenuList() {
menuDiv.appendChild(tbl); // adding table box to main div menuDiv.appendChild(tbl); // adding table box to main div
appedtTo.appendChild(menuDiv); appedtTo.appendChild(menuDiv);
} catch (e) { } catch (e) {
Log("------------------------------------------"); Exception(e, "script error at showing menu list:");
Log("script error at showing menu list:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
} }
} }