mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
Merge branch 'devel'
This commit is contained in:
commit
326587af14
1 changed files with 94 additions and 46 deletions
122
stable.user.js
122
stable.user.js
|
@ -224,8 +224,12 @@
|
|||
|
||||
// : Basic processing helpers {{{
|
||||
|
||||
function getTextPromisesFromNode(node) {
|
||||
return Array.from(node.childNodes).reduce((promises, elem) => {
|
||||
function getTextPromisesFromNode(inputNode) {
|
||||
const nodes = Array.from(inputNode.childNodes)
|
||||
.map((x) => flattenNode(x))
|
||||
.flat()
|
||||
|
||||
return nodes.reduce((promises, elem) => {
|
||||
let img = elem
|
||||
if (elem.tagName !== 'IMG') {
|
||||
const t = elem.tagName ? elem.getElementsByTagName('img') : []
|
||||
|
@ -252,6 +256,9 @@
|
|||
}
|
||||
|
||||
if (img.tagName === 'IMG') {
|
||||
if (img.title) {
|
||||
promises.push({ type: 'txt', val: img.title, node: elem })
|
||||
} else {
|
||||
promises.push(
|
||||
new Promise((resolve) => {
|
||||
digestMessage(getBase64Image(img)).then((res) => {
|
||||
|
@ -263,6 +270,7 @@
|
|||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
} else if (elem.tagName === undefined) {
|
||||
promises.push({ type: 'txt', val: elem.nodeValue, node: elem })
|
||||
} else {
|
||||
|
@ -273,6 +281,16 @@
|
|||
}, [])
|
||||
}
|
||||
|
||||
function flattenNode(node) {
|
||||
if (node.childNodes && node.childNodes.length > 0) {
|
||||
return Array.from(node.childNodes)
|
||||
.map((x) => flattenNode(x))
|
||||
.flat()
|
||||
} else {
|
||||
return node
|
||||
}
|
||||
}
|
||||
|
||||
function makeTextFromElements(acc, item) {
|
||||
if (emptyOrWhiteSpace(item.val)) {
|
||||
return acc
|
||||
|
@ -317,13 +335,13 @@
|
|||
}
|
||||
})
|
||||
} catch (e) {
|
||||
log("Couldn't get images from result (old)")
|
||||
debugLog("Couldn't get images from result (old)")
|
||||
}
|
||||
}
|
||||
|
||||
function getCurrentSubjectName() {
|
||||
if (logElementGetting) {
|
||||
log('getting current subjects name')
|
||||
debugLog('getting current subjects name')
|
||||
}
|
||||
return document.getElementById('page-header').innerText.split('\n')[0] || ''
|
||||
}
|
||||
|
@ -376,7 +394,7 @@
|
|||
uid: getUid(),
|
||||
}
|
||||
|
||||
log('Sent data', sentData)
|
||||
debugLog('Sent data', sentData)
|
||||
post('ask', sentData).then((results) => {
|
||||
removeLoadingMessage()
|
||||
ShowAnswers(
|
||||
|
@ -396,6 +414,26 @@
|
|||
}
|
||||
|
||||
const questionNodeVariants = {
|
||||
hasInformationText: {
|
||||
criteria: () => {
|
||||
const firstChild =
|
||||
document.getElementsByTagName('form')[1].childNodes[0].childNodes[0]
|
||||
if (!firstChild || !firstChild.className.includes('informationitem')) {
|
||||
return false
|
||||
}
|
||||
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
|
||||
).filter((node) => {
|
||||
return !node.className.includes('informationitem')
|
||||
})
|
||||
},
|
||||
},
|
||||
formFirst: {
|
||||
criteria: () => {
|
||||
const questionNodes = Array.from(
|
||||
|
@ -440,6 +478,7 @@
|
|||
`question nodes ${key} criteria was true, but result is an empty array!`
|
||||
)
|
||||
} else {
|
||||
debugLog(`Using question node getter variant: ${key}`)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -726,6 +765,7 @@
|
|||
`result nodes ${key} criteria was true, but result is an empty array!`
|
||||
)
|
||||
} else {
|
||||
debugLog(`Using question node getter variant: ${key}`)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -780,12 +820,13 @@
|
|||
if (getter.requirement(node)) {
|
||||
try {
|
||||
res = getter.getterFunction(node)
|
||||
debugLog(`[Question getter] Using ${key}`)
|
||||
return true
|
||||
} catch (e) {
|
||||
log(`${key} failed`)
|
||||
debugLog(`[Question getter] ${key} failed`)
|
||||
}
|
||||
} else {
|
||||
log(`${key} did not pass`)
|
||||
debugLog(`[Question getter] ${key} did not pass`)
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -806,7 +847,7 @@
|
|||
const possibleAnswers = getPossibleAnswers(node)
|
||||
|
||||
if (!answerPromises || !questionPromises) {
|
||||
log('Answer or question array is empty, skipping question')
|
||||
debugLog('Answer or question array is empty, skipping question')
|
||||
resolve({ success: false })
|
||||
}
|
||||
|
||||
|
@ -831,12 +872,12 @@
|
|||
resolve(result)
|
||||
})
|
||||
.catch((err) => {
|
||||
warn('Error in getQuizFromNode()')
|
||||
warn('Error in getQuizFromNode() (creating question)')
|
||||
warn(err)
|
||||
resolve({ success: false })
|
||||
})
|
||||
} catch (e) {
|
||||
warn('Error in getQuizFromNode()!')
|
||||
warn('Error in getQuizFromNode() (creating promises)')
|
||||
warn(e)
|
||||
warn(node)
|
||||
}
|
||||
|
@ -1105,7 +1146,7 @@
|
|||
],
|
||||
}
|
||||
|
||||
log(sentData)
|
||||
debugLog(sentData)
|
||||
post('isAdding', sentData).then((res) => {
|
||||
ShowSaveQuizDialog(res.success, sentData, res.totalNewQuestions)
|
||||
})
|
||||
|
@ -1144,13 +1185,13 @@
|
|||
prevLength = kkerdesElements.length
|
||||
clearAllMessages()
|
||||
if (determineCurrentSite() === 'TEST') {
|
||||
log('AVR: handling test')
|
||||
debugLog('AVR: handling test')
|
||||
handleAVRQuiz(url)
|
||||
} else if (determineCurrentSite() === 'RESULT') {
|
||||
log('AVR: handling result')
|
||||
debugLog('AVR: handling result')
|
||||
HandleAVRResults(url)
|
||||
} else {
|
||||
log('AVR: handling UI')
|
||||
debugLog('AVR: handling UI')
|
||||
HandleUI()
|
||||
}
|
||||
}
|
||||
|
@ -1179,7 +1220,7 @@
|
|||
testUrl: url,
|
||||
}
|
||||
|
||||
log('Sent data', sentData)
|
||||
debugLog('Sent data', sentData)
|
||||
post('ask', sentData).then((results) => {
|
||||
removeLoadingMessage()
|
||||
ShowAnswers(
|
||||
|
@ -1192,7 +1233,8 @@
|
|||
)
|
||||
})
|
||||
} catch (e) {
|
||||
console.warn(e)
|
||||
debugLog('Error in handleAVRQuiz')
|
||||
debugLog(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1213,14 +1255,14 @@
|
|||
|
||||
function getVideo() {
|
||||
if (logElementGetting) {
|
||||
log('getting video stuff')
|
||||
debugLog('getting video stuff')
|
||||
}
|
||||
return document.getElementsByTagName('video')[0]
|
||||
}
|
||||
|
||||
function getVideoElement() {
|
||||
if (logElementGetting) {
|
||||
log('getting video element')
|
||||
debugLog('getting video element')
|
||||
}
|
||||
return document.getElementById('videoElement').parentNode
|
||||
}
|
||||
|
@ -1398,7 +1440,7 @@
|
|||
return false // TODO :insert real url
|
||||
},
|
||||
action: (url) => {
|
||||
log('Handling canvas quiz')
|
||||
debugLog('Handling canvas quiz')
|
||||
handleCanvasQuiz(url)
|
||||
},
|
||||
},
|
||||
|
@ -1407,7 +1449,7 @@
|
|||
return false // TODO :insert real url
|
||||
},
|
||||
action: (url) => {
|
||||
log('Handling canvas results')
|
||||
debugLog('Handling canvas results')
|
||||
HandleCanvasResults(url)
|
||||
},
|
||||
},
|
||||
|
@ -1416,7 +1458,7 @@
|
|||
return false // TODO :insert real url
|
||||
},
|
||||
action: (url) => {
|
||||
log('Handling canvas default action')
|
||||
debugLog('Handling canvas default action')
|
||||
HandleUI(url)
|
||||
},
|
||||
},
|
||||
|
@ -1444,7 +1486,7 @@
|
|||
return true // TODO :insert real url
|
||||
},
|
||||
action: (url) => {
|
||||
log('Handling AVR default action')
|
||||
debugLog('Handling AVR default action')
|
||||
handleAVRSite(url)
|
||||
},
|
||||
},
|
||||
|
@ -1459,7 +1501,7 @@
|
|||
)
|
||||
},
|
||||
action: () => {
|
||||
log('Handling moodle quiz')
|
||||
debugLog('Handling moodle quiz')
|
||||
handleMoodleQuiz()
|
||||
},
|
||||
},
|
||||
|
@ -1471,7 +1513,7 @@
|
|||
)
|
||||
},
|
||||
action: (url) => {
|
||||
log('Handling moodle results')
|
||||
debugLog('Handling moodle results')
|
||||
HandleMoodleResults(url)
|
||||
},
|
||||
},
|
||||
|
@ -1485,7 +1527,7 @@
|
|||
)
|
||||
},
|
||||
action: (url) => {
|
||||
log('Handling moodle default action')
|
||||
debugLog('Handling moodle default action')
|
||||
HandleUI(url)
|
||||
},
|
||||
},
|
||||
|
@ -1502,7 +1544,7 @@
|
|||
matcher.matchString === 'default' ||
|
||||
matcher.matchString.includes(forcedMatchString)
|
||||
) {
|
||||
log(`trying '${matcher.matchString}'`)
|
||||
debugLog(`trying '${matcher.matchString}'`)
|
||||
if (matcher.testPage && matcher.testPage.match(url)) {
|
||||
matcher.testPage.action(url)
|
||||
return true
|
||||
|
@ -1871,7 +1913,7 @@
|
|||
// }
|
||||
// }>
|
||||
function ShowAnswers(results) {
|
||||
log(results)
|
||||
debugLog(results)
|
||||
try {
|
||||
const answers = results.reduce((acc, res) => {
|
||||
const prepared = PrepareAnswers(res)
|
||||
|
@ -1900,8 +1942,8 @@
|
|||
)
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Error showing answers')
|
||||
console.warn(e)
|
||||
debugLog('Error showing answers')
|
||||
debugLog(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1965,9 +2007,9 @@
|
|||
sentData.subj = getCurrentSubjectName()
|
||||
} catch (e) {
|
||||
sentData.subj = 'NOSUBJ'
|
||||
log('unable to get subject name :c')
|
||||
debugLog('unable to get subject name :c')
|
||||
}
|
||||
log('SENT DATA', sentData)
|
||||
debugLog('SENT DATA', sentData)
|
||||
post('isAdding', sentData).then((res) => {
|
||||
ShowSaveQuizDialog(res.success, sentData, res.totalNewQuestions)
|
||||
})
|
||||
|
@ -2025,8 +2067,8 @@
|
|||
installSource: info().script.updateURL,
|
||||
})
|
||||
} catch (err) {
|
||||
warn('Unexpected error while registering script')
|
||||
log(err)
|
||||
debugLog('Unexpected error while registering script')
|
||||
debugLog(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2059,8 +2101,8 @@
|
|||
video.currentTime -= seekTime
|
||||
}
|
||||
} catch (err) {
|
||||
log('Hotkey error.')
|
||||
log(err.message)
|
||||
debugLog('Hotkey error.')
|
||||
debugLog(err.message)
|
||||
}
|
||||
})
|
||||
var toadd = getVideoElement()
|
||||
|
@ -2405,8 +2447,8 @@
|
|||
|
||||
result.msgContainer.child.msgDiv.elem.innerText = getCurrMsg().m
|
||||
} catch (e) {
|
||||
console.warn('Error in message updating')
|
||||
console.warn(e)
|
||||
debugLog('Error in message updating')
|
||||
debugLog(e)
|
||||
}
|
||||
}
|
||||
updateMessageText()
|
||||
|
@ -2672,6 +2714,12 @@
|
|||
logHelper(console.log, ...arguments)
|
||||
}
|
||||
|
||||
function debugLog() {
|
||||
if (isDevel) {
|
||||
logHelper(console.log, ...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
function Exception(e, msg) {
|
||||
log('------------------------------------------')
|
||||
log(msg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue