mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
Improved test handling
This commit is contained in:
parent
c987a27ee3
commit
14ce201418
1 changed files with 52 additions and 15 deletions
|
@ -166,8 +166,16 @@
|
|||
function getTextPromisesFromNode(node) {
|
||||
const promises = []
|
||||
Array.from(node.childNodes).forEach(elem => {
|
||||
if (elem.tagName === 'IMG') {
|
||||
promises.push(digestMessage(getBase64Image(elem)))
|
||||
let img = elem
|
||||
if (elem.tagName !== 'IMG') {
|
||||
const t = elem.tagName ? elem.getElementsByTagName('img') : []
|
||||
if (t.length > 0) {
|
||||
img = t[0]
|
||||
}
|
||||
}
|
||||
|
||||
if (img.tagName === 'IMG') {
|
||||
promises.push(digestMessage(getBase64Image(img)))
|
||||
} else if (elem.tagName === undefined) {
|
||||
promises.push({ type: 'txt', val: elem.nodeValue })
|
||||
} else {
|
||||
|
@ -246,9 +254,10 @@
|
|||
|
||||
function getPossibleAnswersFromTest(node) {
|
||||
const promises = []
|
||||
const answers = Array.from(
|
||||
node.getElementsByClassName('answer')[0].childNodes
|
||||
)
|
||||
const answerRoot = node.getElementsByClassName('answer')[0]
|
||||
|
||||
if (answerRoot.tagName === 'DIV') {
|
||||
const answers = Array.from(answerRoot.childNodes)
|
||||
|
||||
answers.forEach(answer => {
|
||||
if (answer.tagName) {
|
||||
|
@ -257,6 +266,21 @@
|
|||
})
|
||||
|
||||
return promises
|
||||
} else if (answerRoot.tagName === 'TABLE') {
|
||||
const answers = Array.from(answerRoot.childNodes[0].childNodes)
|
||||
|
||||
answers.forEach(answer => {
|
||||
if (answer.tagName) {
|
||||
promises.push(
|
||||
getTextPromisesFromNode(answer.getElementsByClassName('text')[0])
|
||||
)
|
||||
// here elements with classname 'control' could be added too. Those should be a dropdown,
|
||||
// containing possible choices
|
||||
}
|
||||
})
|
||||
|
||||
return promises
|
||||
}
|
||||
}
|
||||
|
||||
function getQuestionPromiseForSingleQuestion(node) {
|
||||
|
@ -268,19 +292,31 @@
|
|||
|
||||
Promise.all([
|
||||
Promise.all(questionPromises),
|
||||
Promise.all(possibleAnswerPromises),
|
||||
Promise.all(
|
||||
possibleAnswerPromises.map(x => {
|
||||
return Promise.all(x)
|
||||
})
|
||||
),
|
||||
])
|
||||
.then(([question, possibleAnswerArray]) => {
|
||||
console.log(question, possibleAnswerArray)
|
||||
const questionText = question.map(makeTextFromElements).join(' ')
|
||||
const images = getImagesFromElements(question)
|
||||
const data = getDataFromTest(images)
|
||||
const possibleAnswers = possibleAnswerArray.map(x => {
|
||||
return removeUnnecesarySpaces(x.map(makeTextFromElements).join(' '))
|
||||
})
|
||||
let images = getImagesFromElements([
|
||||
...question,
|
||||
...possibleAnswerArray.reduce((acc, x) => {
|
||||
return [...acc, ...x]
|
||||
}, []),
|
||||
])
|
||||
images = uniq(images)
|
||||
const data = getDataFromTest(images, possibleAnswerArray)
|
||||
|
||||
console.log('\n\n\n')
|
||||
console.log(questionText)
|
||||
console.log(images)
|
||||
console.log(data)
|
||||
possibleAnswers.forEach(x => {
|
||||
console.log(x)
|
||||
})
|
||||
|
@ -387,7 +423,7 @@
|
|||
const questionText = question.map(makeTextFromElements).join(' ')
|
||||
const answerText = answer.map(makeTextFromElements).join(' ')
|
||||
let images = getImagesFromElements([...question, ...answer])
|
||||
images = uniq(images)
|
||||
// images = uniq(images)
|
||||
|
||||
resolve({
|
||||
Q: removeUnnecesarySpaces(questionText),
|
||||
|
@ -542,6 +578,7 @@
|
|||
}
|
||||
|
||||
function getBase64Image(img) {
|
||||
console.log(img)
|
||||
const copy = document.createElement('img')
|
||||
copy.src = img.src
|
||||
copy.crossOrigin = 'Anonymous'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue