Added corect source checker, minor ui changes

This commit is contained in:
mrfry 2020-11-20 21:35:13 +01:00
parent dc2cb72dfd
commit 771b37d59f

View file

@ -86,7 +86,7 @@
// forcing pages for testing. unless you test, do not set these to true!
setVal('ISDEVEL', true)
// only one of these should be true for testing
const forceTestPage = true
const forceTestPage = false
const forceResultPage = false
const forceDefaultPage = false
@ -94,6 +94,8 @@
let serverAdress = 'https://qmining.frylabs.net/'
let apiAdress = 'https://api.frylabs.net/'
const ircAddress = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining'
const correctSource =
'https://qmining.frylabs.net/moodle-test-userscript/stable.user.js?up'
const motdShowCount = 3 /* Ammount of times to show motd */
let infoExpireTime = 60 // Every n seconds basic info should be loaded from server
@ -119,7 +121,6 @@
// : Localisation {{{
const huTexts = {
lastChangeLog: '',
fatalError:
'Fatál error. Check console (f12). Kattints az üzenetre az összes kérdés/válaszért manuális kereséshez!',
consoleErrorInfo:
@ -469,7 +470,6 @@
const questionGetters = {
getSimpleQuestion: {
description: 'Basic question getter',
index: 0,
requirement: node => {
return node.getElementsByClassName('qtext').length > 0
},
@ -483,7 +483,6 @@
const answerGetters = {
getSimpleAnswer: {
description: 'Basic answer getter',
index: 0,
requirement: node => {
return node.getElementsByClassName('rightanswer').length > 0
},
@ -494,7 +493,6 @@
},
noCorrect: {
description: 'Gets correct answer, even if the correct is not shown',
index: 2,
requirement: node => {
return (
node.getElementsByClassName('rightanswer').length === 0 &&
@ -529,7 +527,6 @@
},
getDropdownAnswer: {
description: 'Dropdown answer getter',
index: 1,
requirement: node => {
return false
},
@ -540,7 +537,6 @@
},
getTextareaAnswer: {
description: 'Get complex answer',
index: 1,
requirement: node => {
return false
},
@ -778,6 +774,8 @@
function Main() {
'use strict'
// https://qmining.frylabs.net/moodle-test-userscript/stable.user.js?up
console.log('Moodle / E-Learning script')
console.time('main')
timerStarted = true
@ -1019,22 +1017,6 @@
// : }}}
// : Version action functions {{{
function FreshStart() {
var firstRun = getVal('firstRun') // if the current run is the frst
if (firstRun === undefined || firstRun === true) {
setVal('firstRun', false)
ShowHelp() // showing help
document.write(texts.freshStartWarning)
document.close()
throw new Error('something, so this stuff stops')
}
}
// : }}}
// : UI handling {{{
function HandleUI(url) {
// FIXME: normal string building with localisation :/
@ -1084,14 +1066,13 @@
timeout = undefined
}
if (newVersion) {
// --------------------------------------------------------------------------------------------------------------
greetMsg +=
'Verzió frissítve ' +
info().script.version +
'-re. Changelog:\n' +
texts.lastChangeLog
greetMsg += 'Verzió frissítve ' + info().script.version + '-re.'
setVal('lastVerson', info().script.version) // setting lastVersion
}
if (!installedFromCorrectSource(correctSource)) {
greetMsg +=
'\nScriptet nem a weboldalról raktad fel. PLS reinstall (a href = ...)'
}
if (showMOTD) {
greetMsg += '\nMOTD:\n' + motd
timeout = null
@ -1239,6 +1220,33 @@
// : }}}
// : Misc {{{
// : Install source checker {{{
function installedFromCorrectSource(source) {
// TODO: test this
return info().script.updateURL === correctSource
}
// : }}}
// : Version action functions {{{
function FreshStart() {
var firstRun = getVal('firstRun') // if the current run is the frst
if (firstRun === undefined || firstRun === true) {
setVal('firstRun', false)
ShowHelp() // showing help
document.write(texts.freshStartWarning)
document.close()
throw new Error('something, so this stuff stops')
}
}
// : }}}
// : Video hotkey stuff {{{
// this function adds basic hotkeys for video controll.
@ -1279,6 +1287,8 @@
// : }}}
// : }}}
// : Show message, and script menu stuff {{{
function ClearAllMessages() {
@ -1287,6 +1297,7 @@
// shows a message with "msg" text, "matchPercent" tip and transp, and "timeout" time
function ShowMessage(msgItem, timeout, funct) {
// TODO: simple message not selectable
// msgItem help:
// [ [ {}{}{}{} ] [ {}{}{} ] ]
// msgItem[] <- a questions stuff
@ -1294,7 +1305,7 @@
// msgItem[][].p <- a questions precent
// msgItem[][].m <- a questions message
try {
var defMargin = '0px 5px 0px 5px'
var defMargin = '0px 5px'
var isSimpleMessage = false
var simpleMessageText = ''
if (msgItem.isSimple) {
@ -1365,23 +1376,7 @@
}
})
const xrow = document.createElement('div')
xrow.setAttribute('id', 'msgHeader')
SetStyle(xrow, {
height: '20px',
userSelect: 'none',
})
mainDiv.appendChild(xrow)
const headerText = CreateNodeWithText(xrow, 'asd', 'div')
headerText.title = 'Talált kérdés tárgy neve'
SetStyle(headerText, {
padding: '2px 5px',
textAlign: 'center',
display: 'inline',
})
const xButton = CreateNodeWithText(xrow, '❌', 'div')
const xButton = CreateNodeWithText(null, '❌', 'div')
SetStyle(xButton, {
cursor: 'pointer',
position: 'absolute',
@ -1396,14 +1391,20 @@
// ------------------------------------------------------------------
var matchPercent = msgItem[0][0].p
if (isSimpleMessage) {
mainDiv.appendChild(xButton)
var simpleMessageParagrapg = document.createElement('p') // new paragraph
simpleMessageParagrapg.style.margin = defMargin // fancy margin
var mesageNode = document.createElement('p') // new paragraph
mesageNode.innerHTML = simpleMessageText.replace(/\n/g, '</br>')
simpleMessageParagrapg.appendChild(mesageNode)
mesageNode.addEventListener('mousedown', e => {
e.stopPropagation()
})
// TODO: edit this margin to make simple message draggable
SetStyle(mesageNode, {
margin: defMargin,
margin: '10px 0px',
cursor: 'auto',
})
Array.from(mesageNode.getElementsByTagName('a')).forEach(anchorElem => {
@ -1412,6 +1413,23 @@
mainDiv.appendChild(simpleMessageParagrapg) // adding text box to main div
} else {
const headerRow = document.createElement('div')
headerRow.setAttribute('id', 'msgHeader')
SetStyle(headerRow, {
height: '20px',
userSelect: 'none',
})
mainDiv.appendChild(headerRow)
const headerText = CreateNodeWithText(headerRow, '', 'div')
headerText.title = 'Talált kérdés tárgy neve'
SetStyle(headerText, {
padding: '2px 5px',
textAlign: 'center',
display: 'inline',
})
headerRow.appendChild(xButton)
// if its a fucking complicated message
// TABLE SETUP ------------------------------------------------------------------------------------------------------------
var table = document.createElement('table')
@ -1644,6 +1662,18 @@
borderRadius: '5px',
})
const xButton = CreateNodeWithText(menuButtonDiv, '❌', 'div')
SetStyle(xButton, {
cursor: 'pointer',
position: 'absolute',
right: '0px',
display: 'inline',
})
xButton.addEventListener('mousedown', e => {
e.stopPropagation()
menuButtonDiv.parentNode.removeChild(menuButtonDiv)
})
var tbl = document.createElement('table')
tbl.style.margin = '5px 5px 5px 5px'
tbl.style.textAlign = 'left'
@ -1980,7 +2010,9 @@
var paragraphElement = document.createElement(type || 'p') // new paragraph
var textNode = document.createTextNode(text)
paragraphElement.appendChild(textNode)
to.appendChild(paragraphElement)
if (to) {
to.appendChild(paragraphElement)
}
return paragraphElement
}