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) {
|
function getTextPromisesFromNode(node) {
|
||||||
const promises = []
|
const promises = []
|
||||||
Array.from(node.childNodes).forEach(elem => {
|
Array.from(node.childNodes).forEach(elem => {
|
||||||
if (elem.tagName === 'IMG') {
|
let img = elem
|
||||||
promises.push(digestMessage(getBase64Image(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) {
|
} else if (elem.tagName === undefined) {
|
||||||
promises.push({ type: 'txt', val: elem.nodeValue })
|
promises.push({ type: 'txt', val: elem.nodeValue })
|
||||||
} else {
|
} else {
|
||||||
|
@ -246,9 +254,10 @@
|
||||||
|
|
||||||
function getPossibleAnswersFromTest(node) {
|
function getPossibleAnswersFromTest(node) {
|
||||||
const promises = []
|
const promises = []
|
||||||
const answers = Array.from(
|
const answerRoot = node.getElementsByClassName('answer')[0]
|
||||||
node.getElementsByClassName('answer')[0].childNodes
|
|
||||||
)
|
if (answerRoot.tagName === 'DIV') {
|
||||||
|
const answers = Array.from(answerRoot.childNodes)
|
||||||
|
|
||||||
answers.forEach(answer => {
|
answers.forEach(answer => {
|
||||||
if (answer.tagName) {
|
if (answer.tagName) {
|
||||||
|
@ -257,6 +266,21 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
return promises
|
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) {
|
function getQuestionPromiseForSingleQuestion(node) {
|
||||||
|
@ -268,19 +292,31 @@
|
||||||
|
|
||||||
Promise.all([
|
Promise.all([
|
||||||
Promise.all(questionPromises),
|
Promise.all(questionPromises),
|
||||||
Promise.all(possibleAnswerPromises),
|
Promise.all(
|
||||||
|
possibleAnswerPromises.map(x => {
|
||||||
|
return Promise.all(x)
|
||||||
|
})
|
||||||
|
),
|
||||||
])
|
])
|
||||||
.then(([question, possibleAnswerArray]) => {
|
.then(([question, possibleAnswerArray]) => {
|
||||||
|
console.log(question, possibleAnswerArray)
|
||||||
const questionText = question.map(makeTextFromElements).join(' ')
|
const questionText = question.map(makeTextFromElements).join(' ')
|
||||||
const images = getImagesFromElements(question)
|
|
||||||
const data = getDataFromTest(images)
|
|
||||||
const possibleAnswers = possibleAnswerArray.map(x => {
|
const possibleAnswers = possibleAnswerArray.map(x => {
|
||||||
return removeUnnecesarySpaces(x.map(makeTextFromElements).join(' '))
|
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('\n\n\n')
|
||||||
console.log(questionText)
|
console.log(questionText)
|
||||||
console.log(images)
|
console.log(images)
|
||||||
|
console.log(data)
|
||||||
possibleAnswers.forEach(x => {
|
possibleAnswers.forEach(x => {
|
||||||
console.log(x)
|
console.log(x)
|
||||||
})
|
})
|
||||||
|
@ -387,7 +423,7 @@
|
||||||
const questionText = question.map(makeTextFromElements).join(' ')
|
const questionText = question.map(makeTextFromElements).join(' ')
|
||||||
const answerText = answer.map(makeTextFromElements).join(' ')
|
const answerText = answer.map(makeTextFromElements).join(' ')
|
||||||
let images = getImagesFromElements([...question, ...answer])
|
let images = getImagesFromElements([...question, ...answer])
|
||||||
images = uniq(images)
|
// images = uniq(images)
|
||||||
|
|
||||||
resolve({
|
resolve({
|
||||||
Q: removeUnnecesarySpaces(questionText),
|
Q: removeUnnecesarySpaces(questionText),
|
||||||
|
@ -542,6 +578,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBase64Image(img) {
|
function getBase64Image(img) {
|
||||||
|
console.log(img)
|
||||||
const copy = document.createElement('img')
|
const copy = document.createElement('img')
|
||||||
copy.src = img.src
|
copy.src = img.src
|
||||||
copy.crossOrigin = 'Anonymous'
|
copy.crossOrigin = 'Anonymous'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue