Removed lotsa stuff

This commit is contained in:
mrfry 2020-11-14 12:14:30 +01:00
parent 2ae514b1fc
commit 2a612c1bd5

View file

@ -247,7 +247,7 @@
console.warn('NO QUESTION FOR QTEXT') console.warn('NO QUESTION FOR QTEXT')
} }
question = question[0].innerText question = question[0].innerText
return SUtils.RemoveUnnecesarySpaces(question) return removeUnnecesarySpaces(question)
} }
function getBase64Image(img) { function getBase64Image(img) {
@ -298,102 +298,21 @@
return input.replace(/\s/g, ' ') return input.replace(/\s/g, ' ')
} }
// ---------------------------------------------------------------------------------------------- function emptyOrWhiteSpace(value) {
class StringUtils {
removeMultipleEnters(text) {
let res = text.replace(/\t/g, '')
while (res.includes('\n\n')) {
res = res.replace('\n\n', '\n')
}
return res
}
RemoveStuff(value, removableStrings, toReplace) {
removableStrings.forEach(x => {
var regex = new RegExp(x, 'g')
value = value.replace(regex, toReplace || '')
})
return value
}
SimplifyQuery(q) {
assert(q)
var result = q.replace(/\n/g, ' ').replace(/\s/g, ' ')
return this.RemoveUnnecesarySpaces(result)
}
ShortenString(toShorten, ammount) {
assert(toShorten)
var result = ''
var i = 0
while (i < toShorten.length && i < ammount) {
result += toShorten[i]
i++
}
return result
}
ReplaceCharsWithSpace(val, char) {
assert(val)
assert(char)
var toremove = this.NormalizeSpaces(val)
var regex = new RegExp(char, 'g')
toremove = toremove.replace(regex, ' ')
return this.RemoveUnnecesarySpaces(toremove)
}
// removes whitespace from begining and and, and replaces multiple spaces with one space
RemoveUnnecesarySpaces(toremove) {
assert(toremove)
toremove = this.NormalizeSpaces(toremove)
while (toremove.includes(' ')) {
toremove = toremove.replace(/ {2}/g, ' ')
}
return toremove.trim()
}
RemoveSpecialChars(value) {
assert(value)
return this.RemoveStuff(value, specialChars, ' ')
}
// if the value is empty, or whitespace
EmptyOrWhiteSpace(value) {
// replaces /n-s with "". then replaces spaces with "". if it equals "", then its empty, or only consists of white space
if (value === undefined) { if (value === undefined) {
return true return true
} }
return ( return (
value value
.replace(/\n/g, '') .replace(/\n/g, '')
.replace(/\t/g, '')
.replace(/ /g, '') .replace(/ /g, '')
.replace(/\s/g, ' ') === '' .replace(/\s/g, ' ') === ''
) )
} }
// damn nonbreaking space // ----------------------------------------------------------------------------------------------
NormalizeSpaces(input) {
assert(input)
return input.replace(/\s/g, ' ')
}
SimplifyStack(stack) {
return this.SimplifyQuery(stack)
}
}
const SUtils = new StringUtils()
// : }}} // : }}}
@ -530,151 +449,39 @@
return overlayElement return overlayElement
} }
class QuestionsPageModell { // ----------------------------------------------------------------------------------------------
GetAllQuestionsDropdown() { // Misc
if (logElementGetting) { // ----------------------------------------------------------------------------------------------
Log('getting dropdown question')
}
let items = document
.getElementById('responseform')
.getElementsByTagName('p')[0].childNodes
let r = ''
items.forEach(item => {
if (item.tagName === undefined) {
r += item.nodeValue
}
})
return r
}
GetAllQuestionsQtext() { function getCurrentSubjectName() {
if (logElementGetting) {
Log('getting all questions qtext')
}
return document
.getElementById('responseform')
.getElementsByClassName('qtext') // getting questions
}
GetAllQuestionsP() {
if (logElementGetting) {
Log('getting all questions by tag p')
}
return document.getElementById('responseform').getElementsByTagName('p')
}
GetFormulationClearfix() {
if (logElementGetting) {
Log('getting formulation clearfix lol')
}
return document.getElementsByClassName('formulation clearfix')
}
GetAnswerOptions() {
if (logElementGetting) {
Log('getting all answer options')
}
return this.GetFormulationClearfix()[0].childNodes[3].innerText
}
GetQuestionImages() {
if (logElementGetting) {
Log('getting question images')
}
return this.GetFormulationClearfix()[0].getElementsByTagName('img')
}
// this function should return the question, posible answers, and image names
GetQuestionFromTest() {
var questions // the important questions
var allQuestions // all questions
try {
allQuestions = this.GetAllQuestionsQtext() // getting questions
if (allQuestions.length === 0) {
var ddq = this.GetAllQuestionsDropdown()
if (SUtils.EmptyOrWhiteSpace(ddq)) {
var questionData = ''
for (var j = 0; j < allQuestions.length; j++) {
let subAllQuestions = allQuestions[j].childNodes
for (let i = 0; i < subAllQuestions.length; i++) {
if (
subAllQuestions[i].data !== undefined &&
!SUtils.EmptyOrWhiteSpace(subAllQuestions[i].data)
) {
questionData += subAllQuestions[i].data + ' ' // adding text to question data
}
}
}
questions = [questionData]
} else {
questions = [ddq]
}
} else {
questions = []
for (let i = 0; i < allQuestions.length; i++) {
questions.push(allQuestions[i].innerText)
}
}
} catch (e) {
Exception(e, 'script error at getting question:')
}
var imgNodes = '' // the image nodes for questions
try {
imgNodes = this.GetQuestionImages() // getting question images, if there is any
AddImageNamesToImages(imgNodes) // adding image names to images, so its easier to search for, or even guessing
} catch (e) {
Log(e)
Log('Some error with images')
}
questions = questions.map(item => {
if (item) {
return SUtils.ReplaceCharsWithSpace(item, '\n')
}
})
return {
imgnodes: imgNodes,
allQ: allQuestions,
q: questions,
}
}
}
class MiscPageModell {
GetCurrentSubjectName() {
if (logElementGetting) { if (logElementGetting) {
Log('getting current subjects name') Log('getting current subjects name')
} }
return ( return document.getElementById('page-header').innerText.split('\n')[0] || ''
document.getElementById('page-header').innerText.split('\n')[0] || ''
)
} }
GetVideo() { function getVideo() {
if (logElementGetting) { if (logElementGetting) {
Log('getting video stuff') Log('getting video stuff')
} }
return document.getElementsByTagName('video')[0] return document.getElementsByTagName('video')[0]
} }
GetVideoElement() { function getVideoElement() {
if (logElementGetting) { if (logElementGetting) {
Log('getting video element') Log('getting video element')
} }
return document.getElementById('videoElement').parentNode return document.getElementById('videoElement').parentNode
} }
GetInputType(answers, i) { function getInputType(answers, i) {
if (logElementGetting) { if (logElementGetting) {
Log('getting input type') Log('getting input type')
} }
return answers[i].getElementsByTagName('input')[0].type return answers[i].getElementsByTagName('input')[0].type
} }
}
var QPM = new QuestionsPageModell() // ----------------------------------------------------------------------------------------------
var MPM = new MiscPageModell()
// : }}} // : }}}
@ -973,7 +780,7 @@
} }
let showMOTD = false let showMOTD = false
if (!SUtils.EmptyOrWhiteSpace(motd)) { if (!emptyOrWhiteSpace(motd)) {
var prevmotd = getVal('motd') var prevmotd = getVal('motd')
if (prevmotd !== motd) { if (prevmotd !== motd) {
showMOTD = true showMOTD = true
@ -1041,37 +848,35 @@
// : Answering stuffs {{{ // : Answering stuffs {{{
function HandleQuiz() { function HandleQuiz() {
var q = QPM.GetQuestionFromTest() // TODO
var questions = q.q // var q = QPM.GetQuestionFromTest()
var imgNodes = q.imgnodes // var questions = q.q
// ------------------------------------------------------------------------------------------------------ // var imgNodes = q.imgnodes
let promises = [] // // ------------------------------------------------------------------------------------------------------
questions.forEach(x => { // let promises = []
let question = SUtils.EmptyOrWhiteSpace(x) // questions.forEach(x => {
? '' // let question = SUtils.EmptyOrWhiteSpace(x)
: SUtils.RemoveUnnecesarySpaces(x) // simplifying question // ? ''
promises.push( // : SUtils.RemoveUnnecesarySpaces(x) // simplifying question
GetXHRQuestionAnswer({ // promises.push(
q: question, // GetXHRQuestionAnswer({
data: GetImageDataFromImgNodes(imgNodes), // q: question,
subj: MPM.GetCurrentSubjectName(), // data: GetImageDataFromImgNodes(imgNodes),
}) // subj: MPM.GetCurrentSubjectName(),
) // })
}) // )
// })
Promise.all(promises).then(res => { // Promise.all(promises).then(res => {
let answers = [] // let answers = []
// res.forEach((result, j) => {
res.forEach((result, j) => { // var r = PrepareAnswers(result, j)
var r = PrepareAnswers(result, j) // if (r !== undefined) {
if (r !== undefined) { // answers.push(r)
answers.push(r) // }
} // HighLightAnswer(result, j) // highlights the answer for the current result
HighLightAnswer(result, j) // highlights the answer for the current result // })
}) // ShowAnswers(answers, q.q)
// })
ShowAnswers(answers, q.q)
})
} }
function PrepareAnswers(result, j) { function PrepareAnswers(result, j) {
@ -1183,7 +988,7 @@
} }
try { try {
try { try {
sentData.subj = MPM.GetCurrentSubjectName() sentData.subj = getCurrentSubjectName()
} catch (e) { } catch (e) {
sentData.subj = 'NOSUBJ' sentData.subj = 'NOSUBJ'
Log('unable to get subject name :c') Log('unable to get subject name :c')
@ -1207,48 +1012,24 @@
// : Helpers {{{ // : Helpers {{{
function GetImageDataFromImgNodes(imgs) {
var questionImages = [] // the array for the image names in question
for (var i = 0; i < imgs.length; i++) {
if (!imgs[i].src.includes('brokenfile')) {
var filePart = imgs[i].src.split('/') // splits the link by "/"
filePart = filePart[filePart.length - 1] // the last one is the image name
questionImages.push(
decodeURI(
SUtils.RemoveUnnecesarySpaces(SUtils.ShortenString(filePart, 30))
)
) // decodes uri codes, and removes exess spaces, and shortening it
}
}
if (questionImages.length > 0) {
return {
type: 'image',
images: questionImages,
}
} else {
return {
type: 'simple',
}
}
}
// adds image names to image nodes // adds image names to image nodes
function AddImageNamesToImages(imgs) { function AddImageNamesToImages(imgs) {
for (var i = 0; i < imgs.length; i++) { // TODO
if (!imgs[i].src.includes('brokenfile')) { // for (var i = 0; i < imgs.length; i++) {
// TODO: add this to shadowroot // if (!imgs[i].src.includes('brokenfile')) {
var filePart = imgs[i].src.split('/') // splits the link by "/" // // TODO: add this to shadowroot
// console.log(imgs[i].src.split("base64,")[1]) // var filePart = imgs[i].src.split('/') // splits the link by "/"
// TODO: base64 // // console.log(imgs[i].src.split("base64,")[1])
filePart = filePart[filePart.length - 1] // the last one is the image name // // TODO: base64
var appendTo = imgs[i].parentNode // it will be appended here // filePart = filePart[filePart.length - 1] // the last one is the image name
var mainDiv = document.createElement('div') // var appendTo = imgs[i].parentNode // it will be appended here
var fileName = SUtils.ShortenString(decodeURI(filePart), 15) // shortening name, couse it can be long as fuck // var mainDiv = document.createElement('div')
var textNode = document.createTextNode('(' + fileName + ')') // var fileName = SUtils.ShortenString(decodeURI(filePart), 15) // shortening name, couse it can be long as fuck
mainDiv.appendChild(textNode) // var textNode = document.createTextNode('(' + fileName + ')')
appendBelowElement(appendTo, mainDiv) // mainDiv.appendChild(textNode)
} // appendBelowElement(appendTo, mainDiv)
} // }
// }
} }
// this function adds basic hotkeys for video controll. // this function adds basic hotkeys for video controll.
@ -1256,7 +1037,7 @@
var seekTime = 20 var seekTime = 20
document.addEventListener('keydown', function(e) { document.addEventListener('keydown', function(e) {
try { try {
var video = MPM.GetVideo() var video = getVideo()
var keyCode = e.keyCode // getting keycode var keyCode = e.keyCode // getting keycode
if (keyCode === 32) { if (keyCode === 32) {
// if the keycode is 32 (space) // if the keycode is 32 (space)
@ -1280,7 +1061,7 @@
Log(err.message) Log(err.message)
} }
}) })
var toadd = MPM.GetVideoElement() var toadd = getVideoElement()
var node = CreateNodeWithText(toadd, texts.videoHelp) var node = CreateNodeWithText(toadd, texts.videoHelp)
node.style.margin = '5px 5px 5px 5px' // fancy margin node.style.margin = '5px 5px 5px 5px' // fancy margin
} }
@ -1292,11 +1073,11 @@
let maxInd = 4 // inp.length * 0.2; let maxInd = 4 // inp.length * 0.2;
if (dotIndex < maxInd) { if (dotIndex < maxInd) {
return SUtils.RemoveUnnecesarySpaces( return removeUnnecesarySpaces(
inp.substr(inp.indexOf('.') + 1, inp.length) inp.substr(inp.indexOf('.') + 1, inp.length)
) )
} else if (doubledotIndex < maxInd) { } else if (doubledotIndex < maxInd) {
return SUtils.RemoveUnnecesarySpaces( return removeUnnecesarySpaces(
inp.substr(inp.indexOf(':') + 1, inp.length) inp.substr(inp.indexOf(':') + 1, inp.length)
) )
} else { } else {
@ -1307,61 +1088,6 @@
// highlights the possible solutions to the current question // highlights the possible solutions to the current question
function HighLightAnswer(results, currQuestionNumber) { function HighLightAnswer(results, currQuestionNumber) {
// TODO: fix this // TODO: fix this
try {
if (results.length > 0) {
var answers = QPM.GetAllAnswer(currQuestionNumber) // getting all answers
var toColor = [] // the numberth in the array will be colored, and .length items will be colored
var type = '' // type of the question. radio or ticbox or whatitscalled
for (let i = 0; i < answers.length; i++) {
// going thtough answers
if (
answers[i].tagName &&
answers[i].tagName.toLowerCase() === 'div'
) {
// if its not null and is "div"
var correct = results[0].q.A.toLowerCase() // getting current correct answer from data
var answer = answers[i].innerText.replace(/\n/g, '').toLowerCase() // getting current answer
// removing stuff like "a."
answer = RemoveLetterMarking(answer)
if (
SUtils.EmptyOrWhiteSpace(correct) ||
SUtils.EmptyOrWhiteSpace(answer)
) {
continue
}
if (
SUtils.NormalizeSpaces(
SUtils.RemoveUnnecesarySpaces(correct)
).includes(answer)
) {
// if the correct answer includes the current answer
toColor.push(i) // adding the index
type = MPM.GetInputType(answers, i) // setting the type
}
}
}
if (results[0].match === 100) {
// if the result is 100% correct
if (type !== 'radio' || toColor.length === 1) {
// FIXME why not radio
for (let i = 0; i < toColor.length; i++) {
// going through "toColor"
let highlight = createHoverOver(answers[toColor[i]])
Object.assign(highlight.style, {
border: '4px solid rgba(100, 240, 100, 0.8)',
borderRadius: '10px',
})
}
}
} // and coloring the correct index
}
} catch (e) {
// catching errors. Sometimes there are random errors, wich i did not test, but they are rare, and does not break the main script.
Log('script error at highlightin answer: ' + e.message)
}
} }
// : }}} // : }}}
@ -1570,7 +1296,7 @@
const SetQuestionText = () => { const SetQuestionText = () => {
var relevantQuestion = GetRelevantQuestion() var relevantQuestion = GetRelevantQuestion()
questionTextElement.innerText = SUtils.removeMultipleEnters( questionTextElement.innerText = removeUnnecesarySpaces(
relevantQuestion.m relevantQuestion.m
) )
if (currItem === 0 && currRelevantQuestion === 0) { if (currItem === 0 && currRelevantQuestion === 0) {