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