From c59fc4cb4a36a7f527fbd8e040832633d435dacf Mon Sep 17 00:00:00 2001 From: mrfry Date: Thu, 12 Nov 2020 17:11:47 +0100 Subject: [PATCH] Image handling basics --- stable.user.js | 78 ++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/stable.user.js b/stable.user.js index 12741d8..9b10472 100755 --- a/stable.user.js +++ b/stable.user.js @@ -164,16 +164,20 @@ do { const question = getQuestionFromNode(containerNode) const answer = getAnswerFromNode(containerNode) + const image = getImageFromNode(containerNode) result = [ ...result, { Q: question, A: answer, + data: getData(image), }, ] console.log(question) console.log(answer) - console.log('########################') + console.log(image) + console.log('') + console.log('') i++ containerNode = document.getElementById(`q${i}`) @@ -189,6 +193,29 @@ return result } + function getData(image) { + if (image) { + return { + type: 'simple', + } + } else { + return { + type: 'image', + src: image, + } + } + } + + function getImageFromNode(node) { + const img = node.getElementsByTagName('img') + if (img.length === 0) { + return [] + } + return Array.from(img).map(image => { + return image.src + }) + } + function getAnswerFromNode(node) { let answer = node.getElementsByClassName('rightanswer') if (answer.length === 0) { @@ -196,7 +223,7 @@ } answer = answer[0] - return SUtils.RemoveUnnecesarySpaces(answer.innerText) + return SUtils.RemoveUnnecesarySpaces(answer.innerHTML) } function getQuestionFromNode(node) { @@ -1072,50 +1099,6 @@ ) } - // this should get the image url from a result page - // i is the index of the question - // FIXME: move this to RPM class ??? and refactor this - function GetImageFormResult(i) { - try { - var imgElements = RPM.GetResultImage(i) // trying to get image - var imgURL = [] // image urls - for (var j = 0; j < imgElements.length; j++) { - if (!imgElements[j].src.includes('brokenfile')) { - var filePart = imgElements[j].src.split('/') // splits the link by "/" - filePart = filePart[filePart.length - 1] // the last one is the image name - imgURL.push(decodeURI(SUtils.ShortenString(filePart, 30))) - } - } - if (imgURL.length > 0) { - return imgURL - } - } catch (e) { - Log("Couldn't get images from result") - } - } - - function GetDataFormResult(i) { - let data = { type: 'simple' } - - let img = GetImageFormResult(i) - let grabbox = RPM.GetAnswersFromGrabBox(i) - if (img) { - data = { - type: 'image', - images: img, - } - } - if (grabbox) { - data = { - type: 'grabbox', - images: img, - grabbox: grabbox, - } - } - - return data - } - // saves the current quiz. questionData contains the active subjects questions function SaveQuiz(quiz, next) { try { @@ -1248,9 +1231,10 @@ // highlights the possible solutions to the current question function HighLightAnswer(results, currQuestionNumber) { + // TODO: fix this try { if (results.length > 0) { - var answers = RPM.GetAllAnswer(currQuestionNumber) // getting all answers + 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++) {