mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
Image handling in messages
This commit is contained in:
parent
684d92959d
commit
4e64180cb3
1 changed files with 103 additions and 48 deletions
151
stable.user.js
151
stable.user.js
|
@ -199,7 +199,7 @@
|
||||||
resolve({
|
resolve({
|
||||||
type: 'img',
|
type: 'img',
|
||||||
val: res,
|
val: res,
|
||||||
node: elem,
|
node: img,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -229,34 +229,34 @@
|
||||||
function getImagesFromElements(elements) {
|
function getImagesFromElements(elements) {
|
||||||
return elements.reduce((acc, element) => {
|
return elements.reduce((acc, element) => {
|
||||||
if (element.type === 'img') {
|
if (element.type === 'img') {
|
||||||
acc.push(element.val)
|
// FIXME: include check needed?
|
||||||
|
if (!acc.includes(element.val)) {
|
||||||
|
acc.push({ val: element.val, node: element.node })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return acc
|
return acc
|
||||||
}, [])
|
}, [])
|
||||||
}
|
}
|
||||||
|
|
||||||
function oldGetImageFormResult(img) {
|
function getLegacyImageID(imgArray) {
|
||||||
|
// TODO: legacy image ids go into question.images, next to 'val' and 'elem'
|
||||||
try {
|
try {
|
||||||
var imgURL = []
|
return imgArray.map(img => {
|
||||||
for (var j = 0; j < img.length; j++) {
|
if (!img.src.includes('brokenfile')) {
|
||||||
if (!img[j].src.includes('brokenfile')) {
|
let filePart = img.src.split('/')
|
||||||
var filePart = img[j].src.split('/')
|
|
||||||
filePart = filePart[filePart.length - 1]
|
filePart = filePart[filePart.length - 1]
|
||||||
|
|
||||||
// shorten string
|
// shorten string
|
||||||
var result = ''
|
let result = ''
|
||||||
var i = 0
|
let i = 0
|
||||||
while (i < filePart.length && i < 30) {
|
while (i < filePart.length && i < 30) {
|
||||||
result += filePart[i]
|
result += filePart[i]
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
imgURL.push(decodeURI(result))
|
return decodeURI(result)
|
||||||
}
|
|
||||||
}
|
|
||||||
if (imgURL.length > 0) {
|
|
||||||
return imgURL
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log("Couldn't get images from result (old)")
|
log("Couldn't get images from result (old)")
|
||||||
}
|
}
|
||||||
|
@ -308,20 +308,21 @@
|
||||||
data: question.data,
|
data: question.data,
|
||||||
subj: getCurrentSubjectName(),
|
subj: getCurrentSubjectName(),
|
||||||
}
|
}
|
||||||
promises.push(GetXHRQuestionAnswer(questionObj))
|
promises.push(
|
||||||
|
new Promise(resolve => {
|
||||||
|
GetXHRQuestionAnswer(questionObj).then(res => {
|
||||||
|
resolve({
|
||||||
|
answers: res,
|
||||||
|
question: question,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
Promise.all(promises).then(result => {
|
Promise.all(promises).then(results => {
|
||||||
const answers = result.reduce((acc, res) => {
|
|
||||||
const prepared = PrepareAnswers(res)
|
|
||||||
if (prepared) {
|
|
||||||
acc.push(prepared)
|
|
||||||
}
|
|
||||||
return acc
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
removeLoadingMessage()
|
removeLoadingMessage()
|
||||||
ShowAnswers(answers)
|
ShowAnswers(results)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
@ -397,6 +398,23 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getImgNodesFromArray(arr) {
|
||||||
|
return arr.reduce((acc, x) => {
|
||||||
|
if (Array.isArray(x)) {
|
||||||
|
x.forEach(y => {
|
||||||
|
if (y.type === 'img') {
|
||||||
|
acc.push(y.node)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
if (x.type === 'img') {
|
||||||
|
acc.push(x.node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, [])
|
||||||
|
}
|
||||||
|
|
||||||
function getQuestionPromiseForSingleQuestion(node) {
|
function getQuestionPromiseForSingleQuestion(node) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
try {
|
try {
|
||||||
|
@ -430,8 +448,15 @@
|
||||||
return [...acc, ...x]
|
return [...acc, ...x]
|
||||||
}, []),
|
}, []),
|
||||||
])
|
])
|
||||||
images = uniq(images)
|
const imageNodes = getImgNodesFromArray([
|
||||||
const data = getDataFromTest(images, possibleAnswerArray)
|
...question,
|
||||||
|
...possibleAnswerArray,
|
||||||
|
])
|
||||||
|
const data = getDataFromTest(
|
||||||
|
images,
|
||||||
|
possibleAnswerArray,
|
||||||
|
getLegacyImageID(imageNodes)
|
||||||
|
)
|
||||||
|
|
||||||
resolve({
|
resolve({
|
||||||
question: questionText,
|
question: questionText,
|
||||||
|
@ -454,15 +479,22 @@
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDataFromTest(images) {
|
function getDataFromTest(hashedImages, possibleAnswers, legacyImages) {
|
||||||
if (images.length > 0) {
|
if (hashedImages.length > 0) {
|
||||||
return {
|
return {
|
||||||
type: 'image',
|
type: 'image',
|
||||||
images: images,
|
possibleAnswers,
|
||||||
|
images: hashedImages.map(x => {
|
||||||
|
return x.val
|
||||||
|
}),
|
||||||
|
// TODO
|
||||||
|
// images: legacyImages,
|
||||||
|
hashedImages: hashedImages,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
type: 'simple',
|
type: 'simple',
|
||||||
|
possibleAnswers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -864,8 +896,9 @@
|
||||||
|
|
||||||
function appendBelowElement(el, toAppend) {
|
function appendBelowElement(el, toAppend) {
|
||||||
const rect = el.getBoundingClientRect()
|
const rect = el.getBoundingClientRect()
|
||||||
const left = rect.left + window.scrollX
|
const correction = 8
|
||||||
const top = rect.top + window.scrollY
|
const left = rect.left + window.scrollX - correction
|
||||||
|
const top = rect.top + window.scrollY - correction
|
||||||
|
|
||||||
SetStyle(toAppend, {
|
SetStyle(toAppend, {
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
|
@ -1189,33 +1222,56 @@
|
||||||
// : Answering stuffs {{{
|
// : Answering stuffs {{{
|
||||||
|
|
||||||
function PrepareAnswers(result) {
|
function PrepareAnswers(result) {
|
||||||
if (result.length > 0) {
|
const { answers, question } = result
|
||||||
return result.map(res => {
|
if (answers.length > 0) {
|
||||||
let msg = res.q.Q + '\n'
|
return answers.map(answer => {
|
||||||
|
const { Q, A, data } = answer.q
|
||||||
|
let msg = Q + '\n' + A
|
||||||
|
|
||||||
msg += res.q.A
|
// TODO: show 'képek sorrendben' if there are no new kind of image ids
|
||||||
|
if (data.type === 'image') {
|
||||||
if (res.q.data.type === 'image') {
|
question.images.forEach((img, i) => {
|
||||||
msg +=
|
const regex = new RegExp(`\\[${img.val}\\]`, 'g')
|
||||||
'\n\nKépek fenti válaszok sorrendjében: ' +
|
msg = msg.replace(regex, '[' + i.toString() + ']')
|
||||||
res.q.data.images.join(', ') // if it has image part, adding that too
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
m: msg,
|
m: msg,
|
||||||
p: res.match,
|
p: answer.match,
|
||||||
header: res.detailedMatch.matchedSubjName,
|
header: answer.detailedMatch.matchedSubjName,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ShowAnswers(answers) {
|
function addImageIdsToImageNodes(imgs) {
|
||||||
assert(answers)
|
imgs.images.forEach((img, i) => {
|
||||||
|
const text = document.createElement('div')
|
||||||
|
text.innerText = `[${i}]`
|
||||||
|
SetStyle(text, {
|
||||||
|
backgroundColor: '#333',
|
||||||
|
borderRadius: '5px',
|
||||||
|
color: 'white',
|
||||||
|
opacity: 0.7,
|
||||||
|
fontSize: '13px',
|
||||||
|
})
|
||||||
|
appendBelowElement(img.node, text)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function ShowAnswers(results) {
|
||||||
|
const answers = results.reduce((acc, res) => {
|
||||||
|
const prepared = PrepareAnswers(res)
|
||||||
|
addImageIdsToImageNodes(res.question)
|
||||||
|
if (prepared) {
|
||||||
|
acc.push(prepared)
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, [])
|
||||||
|
|
||||||
// TODO: show which image belongs to which text
|
// TODO: show which image belongs to which text
|
||||||
|
|
||||||
console.log(answers)
|
|
||||||
if (answers.length > 0) {
|
if (answers.length > 0) {
|
||||||
ShowMessage(answers)
|
ShowMessage(answers)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1458,8 +1514,7 @@
|
||||||
var mainDiv = document.createElement('div') // the main divider, wich items will be attached to
|
var mainDiv = document.createElement('div') // the main divider, wich items will be attached to
|
||||||
mainDiv.setAttribute('id', 'messageMainDiv')
|
mainDiv.setAttribute('id', 'messageMainDiv')
|
||||||
if (funct) {
|
if (funct) {
|
||||||
// if there is a function as parameter
|
addEventListener(mainDiv, 'click', funct)
|
||||||
addEventListener(mainDiv, 'click', funct) // adding it as click
|
|
||||||
}
|
}
|
||||||
// lotsa crap style
|
// lotsa crap style
|
||||||
SetStyle(mainDiv, {
|
SetStyle(mainDiv, {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue