diff --git a/stable.user.js b/stable.user.js index 270751b..0024d1c 100755 --- a/stable.user.js +++ b/stable.user.js @@ -46,31 +46,33 @@ // : Script header {{{ // ==UserScript== // @name Moodle/Elearning/KMOOC test help -// @version 2.1.3.10 +// @version 2.1.3.15 // @description Online Moodle/Elearning/KMOOC test help // @author MrFry // @match https://elearning.uni-obuda.hu/* // @match https://exam.elearning.uni-obuda.hu/* +// @match https://oktatas.mai.kvk.uni-obuda.hu/* +// @match https://portal.kgk.uni-obuda.hu/* // @match https://mooc.unideb.hu/* // @match https://elearning.unideb.hu/* +// @match https://elearning.med.unideb.hu/* +// @match https://exam.unideb.hu/* // @match https://itc.semmelweis.hu/moodle/* -// @match https://moodle.gtk.uni-pannon.hu/* -// @match https://oktatas.mai.kvk.uni-obuda.hu/* -// @match https://moodle.pte.hu/* // @match https://szelearning.sze.hu/* // @match https://moodle.kre.hu/* // @match https://moodle.pte.hu/* -// @match https://portal.kgk.uni-obuda.hu/* // @match https://elearning.uni-miskolc.hu/* // @match https://elearning.uni-mate.hu/* -// @match https://edu.gpk.bme.hu/* +// @match https://moodle.gtk.uni-pannon.hu/* // @match https://edu.gtk.bme.hu/* +// @match https://edu.gpk.bme.hu/* // @match https://iktblog.hu/* // @match https://moodle.ms.sapientia.ro/* // @match https://moodle.uni-corvinus.hu/* // @match https://v39.moodle.uniduna.hu/* -// @noframes +// @match https://mentok.net/* // @match https://qmining.frylabs.net/* +// @noframes // @run-at document-start // @grant GM_getResourceText // @grant GM_info @@ -146,6 +148,7 @@ var motd = '' var lastestVersion = '' var subjInfo + var uiShowing = true // array, where elems are added to shadow-root, but its position should be at target. var updatableElements = [] // { elem: ..., target: ... } @@ -260,6 +263,10 @@ if (img.title) { promises.push({ type: 'txt', val: img.title, node: elem }) } else { + const originalBase64 = img.src.startsWith('data:image/') + ? img.src + : null + promises.push( new Promise((resolve) => { digestMessage(getBase64Image(img)).then((res) => { @@ -267,6 +274,7 @@ type: 'img', val: res, node: img, + base64: originalBase64, }) }) }) @@ -310,7 +318,11 @@ if (element.type === 'img') { // FIXME: include check needed? if (!acc.includes(element.val)) { - acc.push({ val: element.val, node: element.node }) + acc.push({ + val: element.val, + node: element.node, + base64: element.base64, + }) } } return acc @@ -336,7 +348,8 @@ } }) } catch (e) { - debugLog("Couldn't get images from result (old)") + warn(e) + warn("Couldn't get images from result (old)") } } @@ -395,7 +408,7 @@ uid: getUid(), } - debugLog('Sent data', sentData) + log('Sent data', sentData) post('ask', sentData).then((results) => { removeLoadingMessage() ShowAnswers( @@ -472,7 +485,13 @@ let questionNodes Object.keys(questionNodeVariants).some((key) => { const variant = questionNodeVariants[key] - if (variant.criteria()) { + let criteriaPassed = false + try { + criteriaPassed = variant.criteria() + } catch (e) { + debugLog('Criteria check failed with error (question nodes)', e) + } + if (criteriaPassed) { questionNodes = variant.getter() if (questionNodes.length === 0) { warn( @@ -706,7 +725,7 @@ ...possibleAnswerArray, ]) const data = getDataFromTest( - node, + question, images, getLegacyImageID(imageNodes) ) @@ -732,7 +751,7 @@ }) } - function getDataFromTest(_node, hashedImages, legacyImages) { + function getDataFromTest(questions, hashedImages, legacyImages) { if (hashedImages.length > 0) { return { type: 'image', @@ -740,6 +759,11 @@ return x.val }), images: legacyImages, + base64: questions + .map((x) => { + return x.base64 + }) + .filter((x) => !!x), } } else { return { @@ -763,7 +787,7 @@ try { criteriaPassed = variant.criteria() } catch (e) { - debugLog('Criteria check failed with error', e) + debugLog('Criteria check failed with error (result nodes)', e) } if (criteriaPassed) { resultNodes = variant.getter() @@ -1153,7 +1177,7 @@ ], } - debugLog(sentData) + log(sentData) post('isAdding', sentData).then((res) => { ShowSaveQuizDialog(res.success, sentData, res.totalNewQuestions) }) @@ -1227,7 +1251,7 @@ testUrl: url, } - debugLog('Sent data', sentData) + log('Sent data', sentData) post('ask', sentData).then((results) => { removeLoadingMessage() ShowAnswers( @@ -1240,8 +1264,8 @@ ) }) } catch (e) { - debugLog('Error in handleAVRQuiz') - debugLog(e) + warn('Error in handleAVRQuiz') + warn(e) } } @@ -1258,6 +1282,7 @@ } // : }}} + // : Misc {{{ function getVideo() { @@ -1427,9 +1452,17 @@ // window.addEventListener("load", () => {}) Main() + function preventWindowClose() { + usf.close = () => { + log('Prevented window.close() ...') + } + } + function Main() { 'use strict' + log('Moodle / E-Learning script') + preventWindowClose() console.log( '%c Moodle / E-Learning script', 'font-weight: bold; font-size: 50px;color: yellow; text-shadow: 3px 3px 0 rgb(245,221,8) , 6px 6px 0 rgb(226,91,14) , 9px 9px 0 rgb(217,31,38) , 12px 12px 0 rgb(5,148,68) , 15px 15px 0 rgb(2,135,206) , 18px 18px 0 rgb(4,77,145) , 21px 21px 0 rgb(42,21,113)' @@ -1658,6 +1691,7 @@ } VersionActions() if (!url.includes('.pdf')) { + SetupMenu() ShowMenu() } ConnectToServer(AfterLoad) @@ -1722,6 +1756,7 @@ }) .catch(() => { NoServerAction() + warn(texts.noServerConsoleMessage) }) } @@ -1747,7 +1782,6 @@ SafeGetElementById('retryContainer', (elem) => { elem.style.display = 'flex' }) - log(texts.noServerConsoleMessage) } function VersionActions() { @@ -1923,7 +1957,7 @@ // } // }> function ShowAnswers(results) { - debugLog(results) + log(results) try { const answers = results.reduce((acc, res) => { const prepared = PrepareAnswers(res) @@ -1952,8 +1986,8 @@ ) } } catch (e) { - debugLog('Error showing answers') - debugLog(e) + warn('Error showing answers') + warn(e) } } @@ -2017,9 +2051,9 @@ sentData.subj = getCurrentSubjectName() } catch (e) { sentData.subj = 'NOSUBJ' - debugLog('unable to get subject name :c') + warn('unable to get subject name :c') } - debugLog('SENT DATA', sentData) + log('SENT DATA', sentData) post('isAdding', sentData).then((res) => { ShowSaveQuizDialog(res.success, sentData, res.totalNewQuestions) }) @@ -2077,8 +2111,8 @@ installSource: info().script.updateURL, }) } catch (err) { - debugLog('Unexpected error while registering script') - debugLog(err) + warn('Unexpected error while registering script') + warn(err) } } @@ -2111,8 +2145,8 @@ video.currentTime -= seekTime } } catch (err) { - debugLog('Hotkey error.') - debugLog(err.message) + warn('Hotkey error.') + warn(err.message) } }) var toadd = getVideoElement() @@ -2212,6 +2246,34 @@ }) } + function ToggleUIElements() { + const newVal = uiShowing ? 'none' : '' + SafeGetElementById( + 'scriptMessage', + (elem) => { + elem.style.display = newVal + }, + true + ) + SafeGetElementById( + 'scriptMenuDiv', + (elem) => { + elem.style.display = newVal + }, + true + ) + uiShowing = !uiShowing + } + + function SetupMenu() { + document.addEventListener('keydown', (event) => { + if (event.ctrlKey && event.key === 'h') { + event.preventDefault() + ToggleUIElements() + } + }) + } + function ShowMessage(msgItem, timeout, funct) { let isSimpleMessage = false let simpleMessageText = '' @@ -2457,8 +2519,8 @@ result.msgContainer.child.msgDiv.elem.innerText = getCurrMsg().m } catch (e) { - debugLog('Error in message updating') - debugLog(e) + warn('Error in message updating') + warn(e) } } updateMessageText() @@ -2710,23 +2772,23 @@ } } - function logHelper(logMethod, ...value) { + function logHelper(logMethod, style, ...value) { if (logEnabled) { - logMethod('[Moodle Script]: ', ...value) + logMethod('%c[Moodle Script]:', style, ...value) } } function warn(value) { - logHelper(console.warn, value) + logHelper(console.warn, 'color:yellow', value) } function log() { - logHelper(console.log, ...arguments) + logHelper(console.log, 'color:green', ...arguments) } function debugLog() { if (isDevel) { - logHelper(console.log, ...arguments) + logHelper(console.log, 'color:grey', ...arguments) } } @@ -2758,11 +2820,12 @@ } } - function SafeGetElementById(id, next) { + function SafeGetElementById(id, next, silent) { const element = overlay.querySelector('#' + id) if (element) { next(element) } else { + if (silent) return log(`Unable to safe get element by id: ${id}`) } }