Small code refactors

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

234
main.js
View file

@ -22,7 +22,6 @@
// TODO:
// 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
@ -32,11 +31,11 @@ var serverAdress = "https://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 = false;
const forceTestPage = true; // TODO: set to false
const forceResultPage = false;
const forceDefaultPage = false;
const logElementGetting = false;
const log = false;
const log = true; // TODO: this is false
var motdShowCount = 3;
var motd = "";
@ -231,6 +230,7 @@ class QuestionDB {
//: Main function {{{
function Main() {
'use strict';
console.clear(); // TODO: remove dis
Init(function(count, subjCount) {
var url = location.href;
try {
@ -251,12 +251,7 @@ function Main() {
active: true
});
});
Log("------------------------------------------");
Log("script error at main:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at main:");
}
if (url.includes("eduplayer")) // if the current site is a video site
AddVideoHotkeys(url); // adding video hotkeys
@ -501,12 +496,7 @@ function GetQuestionFromTest() {
}
}
} catch (e) {
Log("------------------------------------------");
Log("script error at getting question:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at getting question:");
}
var imgNodes = ""; // the image nodes for questions
try {
@ -547,7 +537,7 @@ function GetQuestionFromResult(i) {
}
} else {
try {
currQuestion = GetCurrSubj(i);
currQuestion = GetCurrQuestion(i);
} catch (e) {
currQuestion = "REEEEEEEEEEEEEEEEEEEEE"; // this shouldnt really happen sry guys
}
@ -665,19 +655,12 @@ function Init(cwith) {
}
}());
} catch (e) {
Log("------------------------------------------");
Log("script error at addEventListener:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at addEventListener:");
}
VersionActions();
count = Load(cwith); // loads resources
if (!url.includes(".pdf")) // showing menu unless its a .pdf page
{
if (!url.includes(".pdf"))
ShowMenu();
}
return count;
}
@ -729,12 +712,7 @@ function ReadFile(cwith) {
};
}
} catch (e) {
Log("------------------------------------------");
Log("script error at reading file:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at reading file:");
}
NLoad(resource, cwith);
}
@ -758,12 +736,7 @@ function ReadNetDB(cwith, useNetDB) {
Log("Sending XMLHTTP Request...");
return NewXMLHttpRequest();
} catch (e) {
Log("------------------------------------------");
Log("script error at reading online database:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at reading online database:");
}
}
@ -929,15 +902,7 @@ function NLoad(resource, cwith) {
}
data = r;
count = allCount + 1; // couse starting with -1 to show errors
} catch (e) {
Log("------------------------------------------");
Log("script error at loading:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
count = -1; // returns -1 if error
}
var i = 0;
while (i < data.length && !GM_getValue("Is" + i + "Active")) {
i++;
@ -945,72 +910,16 @@ function NLoad(resource, cwith) {
if (i >= data.length)
document.getElementById("HelperMenuButton").style.background = "yellow";
} catch (e) {
Exception(e, "script error at loading:");
count = -1; // returns -1 if error
}
cwith(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
}
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
});
});
}
}
//: UI handling {{{
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
@ -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 {{{
function HandleResults(url) {
@ -1223,17 +1196,11 @@ function SaveQuiz(quiz, questionData) {
sendSuccess = true;
}
} catch (e) {
Log("error at sending data to server.");
Log(e);
Exception(e, "error at sending data to server.");
}
ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentData);
} catch (e) {
Log("------------------------------------------");
Log("script error at saving quiz:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at saving quiz");
}
}
@ -1270,12 +1237,7 @@ function GetQuiz() {
}
return quiz;
} catch (e) {
Log("------------------------------------------");
Log("script error at quiz parsing:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at quiz parsing:");
}
}
@ -1431,6 +1393,15 @@ function Log(value) {
console.log(value);
}
function Error(e, msg){
Log("------------------------------------------");
Log(msg);
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
}
//: }}}
//: Minor UI stuff {{{
@ -1649,12 +1620,7 @@ function ShowMessage(msgItem, timeout, funct) {
}
});
} catch (e) {
Log("------------------------------------------");
Log("script error at showing message:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at showing message:");
}
}
@ -1707,12 +1673,7 @@ function ShowMenu() {
appedtTo.appendChild(menuButtonDiv);
} catch (e) {
Log("------------------------------------------");
Log("script error at showing menu:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at showing menu:");
}
}
@ -1935,12 +1896,7 @@ function ShowMenuList() {
menuDiv.appendChild(tbl); // adding table box to main div
appedtTo.appendChild(menuDiv);
} catch (e) {
Log("------------------------------------------");
Log("script error at showing menu list:");
Log(e.message);
Log("------------------------------------------");
Log(e.stack);
Log("------------------------------------------");
Exception(e, "script error at showing menu list:");
}
}