mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
Finding question / answer node containers more dinamically
This commit is contained in:
parent
54ea56343c
commit
fc5304667b
1 changed files with 92 additions and 17 deletions
109
stable.user.js
109
stable.user.js
|
@ -351,6 +351,7 @@
|
|||
getQuizData()
|
||||
.then(readQuestions => {
|
||||
if (readQuestions.length === 0) {
|
||||
warn('readQuestions length is zero, no questions found on page!')
|
||||
ShowMessage(
|
||||
texts.unableToParseTestQuestion,
|
||||
|
||||
|
@ -401,20 +402,68 @@
|
|||
})
|
||||
}
|
||||
|
||||
const questionNodeVariants = {
|
||||
formFirst: {
|
||||
criteria: () => {
|
||||
const questionNodes = Array.from(
|
||||
document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
||||
)
|
||||
// test: e2c01ff4-d97a-4ab9-8f7f-e28812541097
|
||||
const notOnlyTextNodes = questionNodes.every(node => {
|
||||
return node.tagName !== undefined
|
||||
})
|
||||
return notOnlyTextNodes && questionNodes.length > 0
|
||||
},
|
||||
getter: () => {
|
||||
return Array.from(
|
||||
document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
||||
)
|
||||
},
|
||||
},
|
||||
formSecond: {
|
||||
criteria: () => {
|
||||
const questionNodes = Array.from(
|
||||
document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
||||
)
|
||||
return questionNodes.length > 0
|
||||
},
|
||||
getter: () => {
|
||||
return Array.from(
|
||||
document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
function getQuestionNodes() {
|
||||
try {
|
||||
let questionNodes
|
||||
Object.keys(questionNodeVariants).some(key => {
|
||||
const variant = questionNodeVariants[key]
|
||||
if (variant.criteria()) {
|
||||
questionNodes = variant.getter()
|
||||
if (questionNodes.length === 0) {
|
||||
warn(
|
||||
`question nodes ${key} criteria was true, but result is an empty array!`
|
||||
)
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
})
|
||||
return questionNodes
|
||||
} catch (e) {
|
||||
warn('Error in getQuestionNodes')
|
||||
warn(e)
|
||||
}
|
||||
}
|
||||
|
||||
function getQuizData() {
|
||||
return new Promise(resolve => {
|
||||
// TODO: dropdown in question
|
||||
// TODO: get possible answers too
|
||||
const promises = []
|
||||
let questionNodes = Array.from(
|
||||
document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
||||
)
|
||||
|
||||
if (questionNodes.length === 0) {
|
||||
questionNodes = Array.from(
|
||||
document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
||||
)
|
||||
}
|
||||
const questionNodes = getQuestionNodes()
|
||||
|
||||
let i = 0
|
||||
while (
|
||||
|
@ -667,17 +716,43 @@
|
|||
|
||||
// : Result page processing functions {{{
|
||||
|
||||
const resultNodeVariants = questionNodeVariants
|
||||
|
||||
function getResultNodes() {
|
||||
try {
|
||||
let resultNodes
|
||||
Object.keys(resultNodeVariants).some(key => {
|
||||
const variant = resultNodeVariants[key]
|
||||
if (variant.criteria()) {
|
||||
resultNodes = variant.getter()
|
||||
if (resultNodes.length === 0) {
|
||||
warn(
|
||||
`result nodes ${key} criteria was true, but result is an empty array!`
|
||||
)
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
})
|
||||
return resultNodes
|
||||
} catch (e) {
|
||||
warn('Error in getResultNodes')
|
||||
warn(e)
|
||||
}
|
||||
}
|
||||
|
||||
function getQuiz() {
|
||||
return new Promise(resolve => {
|
||||
const promises = []
|
||||
let questionNodes = Array.from(
|
||||
document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
||||
)
|
||||
if (questionNodes.length === 0) {
|
||||
questionNodes = Array.from(
|
||||
document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
||||
)
|
||||
}
|
||||
const questionNodes = getResultNodes()
|
||||
// let questionNodes = Array.from(
|
||||
// document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
||||
// )
|
||||
// if (questionNodes.length === 0) {
|
||||
// questionNodes = Array.from(
|
||||
// document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
||||
// )
|
||||
// }
|
||||
|
||||
let i = 0
|
||||
while (i < questionNodes.length && questionNodes[i].tagName === 'DIV') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue