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()
|
getQuizData()
|
||||||
.then(readQuestions => {
|
.then(readQuestions => {
|
||||||
if (readQuestions.length === 0) {
|
if (readQuestions.length === 0) {
|
||||||
|
warn('readQuestions length is zero, no questions found on page!')
|
||||||
ShowMessage(
|
ShowMessage(
|
||||||
texts.unableToParseTestQuestion,
|
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() {
|
function getQuizData() {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
// TODO: dropdown in question
|
// TODO: dropdown in question
|
||||||
// TODO: get possible answers too
|
// TODO: get possible answers too
|
||||||
const promises = []
|
const promises = []
|
||||||
let questionNodes = Array.from(
|
const questionNodes = getQuestionNodes()
|
||||||
document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
|
||||||
)
|
|
||||||
|
|
||||||
if (questionNodes.length === 0) {
|
|
||||||
questionNodes = Array.from(
|
|
||||||
document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let i = 0
|
let i = 0
|
||||||
while (
|
while (
|
||||||
|
@ -667,17 +716,43 @@
|
||||||
|
|
||||||
// : Result page processing functions {{{
|
// : 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() {
|
function getQuiz() {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const promises = []
|
const promises = []
|
||||||
let questionNodes = Array.from(
|
const questionNodes = getResultNodes()
|
||||||
document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
// let questionNodes = Array.from(
|
||||||
)
|
// document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
||||||
if (questionNodes.length === 0) {
|
// )
|
||||||
questionNodes = Array.from(
|
// if (questionNodes.length === 0) {
|
||||||
document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
// questionNodes = Array.from(
|
||||||
)
|
// document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
||||||
}
|
// )
|
||||||
|
// }
|
||||||
|
|
||||||
let i = 0
|
let i = 0
|
||||||
while (i < questionNodes.length && questionNodes[i].tagName === 'DIV') {
|
while (i < questionNodes.length && questionNodes[i].tagName === 'DIV') {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue