mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
Refactored NLoad to work with JSON data
This commit is contained in:
parent
a26830c078
commit
b3389c2b46
1 changed files with 137 additions and 104 deletions
245
main.js
245
main.js
|
@ -34,6 +34,122 @@ const logElementGetting = false;
|
||||||
var motdShowCount = 3;
|
var motdShowCount = 3;
|
||||||
var motd = "";
|
var motd = "";
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
|
// Class descriptions
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class Question {
|
||||||
|
constructor(q, a, i) {
|
||||||
|
this.Q = q;
|
||||||
|
this.A = a;
|
||||||
|
this.I = i;
|
||||||
|
}
|
||||||
|
toString() {
|
||||||
|
var r = "?" + this.Q + "\n!" + this.A;
|
||||||
|
if (this.I)
|
||||||
|
r += "\n>" + this.I;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
HasQuestion() {
|
||||||
|
return this.Q != undefined;
|
||||||
|
}
|
||||||
|
HasAnswer() {
|
||||||
|
return this.A != undefined;
|
||||||
|
}
|
||||||
|
HasImage() {
|
||||||
|
return this.I != undefined;
|
||||||
|
}
|
||||||
|
IsComplete() {
|
||||||
|
return this.HasQuestion() && this.HasAnswer();
|
||||||
|
}
|
||||||
|
Compare(q2) {
|
||||||
|
const qmatchpercent = Question.CompareString(this.Q, q2.Q);
|
||||||
|
const amatchpercent = Question.CompareString(this.A, q2.A);
|
||||||
|
if (this.I != undefined) {
|
||||||
|
const imatchpercent = this.I == undefined ? Question.CompareString(this.I, q2.I) : 0;
|
||||||
|
return (qmatchpercent + amatchpercent + imatchpercent) / 3;
|
||||||
|
} else {
|
||||||
|
return (qmatchpercent + amatchpercent) / 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static CompareString(s1, s2) {
|
||||||
|
//if (s1 == undefined || s2 == undefined)
|
||||||
|
// return 0;
|
||||||
|
s1 = SimplifyStringForComparison(s1).split(" ");
|
||||||
|
s2 = SimplifyStringForComparison(s2).split(" ");
|
||||||
|
var match = 0;
|
||||||
|
for (var i = 0; i < s1.length; i++)
|
||||||
|
if (s2.includes(s1[i]))
|
||||||
|
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;
|
||||||
|
if (percent < 0)
|
||||||
|
percent = 0;
|
||||||
|
return percent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Subject {
|
||||||
|
constructor(n) {
|
||||||
|
this.Name = n;
|
||||||
|
this.Questions = [];
|
||||||
|
}
|
||||||
|
get length() {
|
||||||
|
return this.Questions.length;
|
||||||
|
}
|
||||||
|
AddQuestion(q) {
|
||||||
|
this.Questions.push(q);
|
||||||
|
}
|
||||||
|
toString() {
|
||||||
|
var r = [];
|
||||||
|
for (var i = 0; i < this.Questions.length; i++)
|
||||||
|
r.push(this.Questions[i].toString());
|
||||||
|
return "+" + this.Name + "\n" + r.join("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class QuestionDB {
|
||||||
|
constructor() {
|
||||||
|
this.Subjects = [];
|
||||||
|
}
|
||||||
|
get length() {
|
||||||
|
return this.Subjects.length;
|
||||||
|
}
|
||||||
|
AddQuestion(subj, q) {
|
||||||
|
var i = 0;
|
||||||
|
while (i < this.Subjects.length && this.Subjects[i].Name != subj)
|
||||||
|
i++;
|
||||||
|
if (i < this.Subjects.length)
|
||||||
|
this.Subjects[i].AddQuestion(q);
|
||||||
|
else {
|
||||||
|
const n = new Subject(subj);
|
||||||
|
n.AddQuestion(q);
|
||||||
|
this.Subjects.push(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AddSubject(subj) {
|
||||||
|
var i = 0;
|
||||||
|
while (i < this.length && subj.Name != this.Subjects[i].Name)
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (i < this.length) {
|
||||||
|
this.Subjects.concat(subj.Questions);
|
||||||
|
} else {
|
||||||
|
this.Subjects.push(subj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
toString() {
|
||||||
|
var r = [];
|
||||||
|
for (var i = 0; i < this.Subjects.length; i++)
|
||||||
|
r.push(this.Subjects[i].toString());
|
||||||
|
return r.join("\n\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
|
// Main function
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
function Main() {
|
function Main() {
|
||||||
'use strict';
|
'use strict';
|
||||||
Init(function(count) {
|
Init(function(count) {
|
||||||
|
@ -706,11 +822,7 @@ function ReadFile(cwith) {
|
||||||
|
|
||||||
function ReadNetDB(cwith, useNetDB) {
|
function ReadNetDB(cwith, useNetDB) {
|
||||||
function NewXMLHttpRequest() {
|
function NewXMLHttpRequest() {
|
||||||
var url = "";
|
const url = serverAdress + "data.json";
|
||||||
if (useNetDB == 0) // safe
|
|
||||||
url = serverAdress + "static";
|
|
||||||
else if (useNetDB == 1) // unsafe
|
|
||||||
url = serverAdress + "public";
|
|
||||||
GM_xmlhttpRequest({
|
GM_xmlhttpRequest({
|
||||||
method: "GET",
|
method: "GET",
|
||||||
synchronous: true,
|
synchronous: true,
|
||||||
|
@ -747,9 +859,7 @@ function Load(cwith) {
|
||||||
|
|
||||||
function LoadMOTD(resource) {
|
function LoadMOTD(resource) {
|
||||||
try {
|
try {
|
||||||
if (resource[0][0] == '@')
|
motd = resource.substr;
|
||||||
motd = resource[0].substr(1).replace(/#/g, "\n");
|
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Error loading motd :c");
|
console.log("Error loading motd :c");
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
@ -761,107 +871,30 @@ function LoadMOTD(resource) {
|
||||||
function NLoad(resource, cwith) {
|
function NLoad(resource, cwith) {
|
||||||
var count = -1;
|
var count = -1;
|
||||||
try {
|
try {
|
||||||
var allCount = 0;
|
var data = JSON.parse(resource);
|
||||||
var currIndex = -1;
|
var r = new QuestionDB();
|
||||||
resource = resource.split("\n"); // splitting by enters
|
var rt = [];
|
||||||
|
var allCount = -1;
|
||||||
LoadMOTD(resource);
|
LoadMOTD(resource);
|
||||||
data = []; // initializing data declared at the begining
|
|
||||||
|
|
||||||
function AddNewSubj(name) // adds a new subject to the data
|
for (var i = 0; i < data.Subjects.length; i++) {
|
||||||
{
|
let s = new Subject(data.Subjects[i].Name);
|
||||||
var i = 0;
|
var j = 0;
|
||||||
while (i < data.length && data[i].name != RemoveUnnecesarySpaces(name))
|
for (j = 0; j < data.Subjects[i].Questions.length; j++) {
|
||||||
i++;
|
var currQ = data.Subjects[i].Questions[j];
|
||||||
|
s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I));
|
||||||
|
}
|
||||||
|
rt.push({
|
||||||
|
name: data.Subjects[i].Name,
|
||||||
|
count: j
|
||||||
|
});
|
||||||
|
allCount += j;
|
||||||
|
r.AddSubject(s);
|
||||||
|
}
|
||||||
|
console.log(r);
|
||||||
|
console.log(rt);
|
||||||
|
|
||||||
if (i < data.length) {
|
count = allCount + 1; // couse starting with -1 to show errors
|
||||||
currIndex = i;
|
|
||||||
} else {
|
|
||||||
data.push({
|
|
||||||
"questions": []
|
|
||||||
}); // ads a new object, with an empty array in it
|
|
||||||
currIndex = data.length - 1;
|
|
||||||
GetCurrSubj().name = RemoveUnnecesarySpaces(name); // sets the name for it
|
|
||||||
GetCurrSubj().count = 0; // setting count to default
|
|
||||||
// setting if its active only if its undefined, otherwise previous user setting shouldt be overwritten
|
|
||||||
if (GM_getValue("Is" + (data.length - 1) + "Active") == undefined) {
|
|
||||||
GM_setValue("Is" + (data.length - 1) + "Active", true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function AddItem() // adds an item to the last subjects questions
|
|
||||||
{
|
|
||||||
GetCurrSubj().count++;
|
|
||||||
allCount++; // incrementing all count
|
|
||||||
GetCurrSubj().questions.push({}); // adding a new empty object to the last item in the data
|
|
||||||
}
|
|
||||||
|
|
||||||
function GetLastItem() // returns the last item of the last item of data
|
|
||||||
{
|
|
||||||
var q = GetCurrSubj().questions.length; // questions length
|
|
||||||
return GetCurrSubj().questions[q - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
function GetCurrSubj() {
|
|
||||||
return data[currIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
// ? : question
|
|
||||||
// ! : answer
|
|
||||||
// > : image JSON data
|
|
||||||
// + : subject name
|
|
||||||
|
|
||||||
// checking for name
|
|
||||||
for (var j = 0; j < resource.length; j++) // goes through resources
|
|
||||||
{
|
|
||||||
if (resource[j][0] == '+') // if there is a name identifier
|
|
||||||
{
|
|
||||||
break; // breaks, couse there will be a name
|
|
||||||
}
|
|
||||||
if (resource[j][0] == '?' || resource[j][0] == '!' || resource[j][0] == '>') // if it begins with another identifier:
|
|
||||||
{
|
|
||||||
AddNewSubj("NONAME"); // there is no name (for the first question at least), so setting it noname.
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// else it does nothing, continues to check
|
|
||||||
}
|
|
||||||
var jumped = 0; // the amount of lines it processed
|
|
||||||
console.log("itt lehet néhány data warning, ami nem feltétlen baj, de azért megnézheted.");
|
|
||||||
for (var i = 0; i < resource.length; i += jumped) // gouing through the resource
|
|
||||||
{
|
|
||||||
jumped = 0; // resetting it to 0
|
|
||||||
var currRawDataQ = resource[i]; // current question
|
|
||||||
var currRawDataA = resource[i + 1]; // current answer
|
|
||||||
var currRawDataI = resource[i + 2]; // current image
|
|
||||||
//console.log (currRawDataQ + " " + currRawDataA + " " + currRawDataI)
|
|
||||||
if (currRawDataQ != undefined && currRawDataQ[0] == '?' && currRawDataA != undefined &&
|
|
||||||
currRawDataA[0] == '!') // if the current line is ? and the next is ! its a data
|
|
||||||
{
|
|
||||||
AddItem();
|
|
||||||
GetLastItem().q = currRawDataQ.substr(1);
|
|
||||||
GetLastItem().a = currRawDataA.substr(1);
|
|
||||||
jumped += 2;
|
|
||||||
if (currRawDataI != undefined && currRawDataI[0] == '>') {
|
|
||||||
GetLastItem().i = currRawDataI.substr(1);
|
|
||||||
jumped++;
|
|
||||||
}
|
|
||||||
} else if (currRawDataQ[0] == '+') // if its a new subject
|
|
||||||
{
|
|
||||||
AddNewSubj(currRawDataQ.substr(1));
|
|
||||||
jumped++;
|
|
||||||
} else {
|
|
||||||
// this should be invalid question order
|
|
||||||
console.log("Data Warning: " + currRawDataQ + " " + currRawDataA + " " + currRawDataI);
|
|
||||||
jumped++;
|
|
||||||
}
|
|
||||||
} // end of parsing all data
|
|
||||||
var j = (data.length); // starting from the item after the last
|
|
||||||
// setting all defined values to undefined, so nothing strange happens
|
|
||||||
while (GM_getValue("Is" + j + "Active") != undefined) {
|
|
||||||
GM_setValue("Is" + j + "Active", undefined);
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
count = allCount;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("------------------------------------------");
|
console.log("------------------------------------------");
|
||||||
console.log("script error at loading:");
|
console.log("script error at loading:");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue