Script registering, cid and uid storing fixes

This commit is contained in:
mrfry 2021-01-11 09:28:42 +01:00
parent 17877b8053
commit 0d9f7df3de

View file

@ -112,8 +112,8 @@
// forcing pages for testing. unless you test, do not set these to true! // forcing pages for testing. unless you test, do not set these to true!
setVal('ISDEVEL', true) setVal('ISDEVEL', true)
// only one of these should be true for testing // only one of these should be true for testing
const forceTestPage = false const forceTestPage = true
const forceResultPage = true const forceResultPage = false
const forceDefaultPage = false const forceDefaultPage = false
var addEventListener // add event listener function var addEventListener // add event listener function
@ -127,8 +127,6 @@
const messageOpacityDelta = 0.1 const messageOpacityDelta = 0.1
const minMessageOpacity = 0.2 const minMessageOpacity = 0.2
let infoExpireTime = 60 // Every n seconds basic info should be loaded from server let infoExpireTime = 60 // Every n seconds basic info should be loaded from server
var uid = 0
var cid = 0
var motd = '' var motd = ''
var userSpecificMotd = '' var userSpecificMotd = ''
var lastestVersion = '' var lastestVersion = ''
@ -272,7 +270,6 @@
} }
function getLegacyImageID(imgArray) { function getLegacyImageID(imgArray) {
// TODO: legacy image ids go into question.images, next to 'val' and 'elem'
try { try {
return imgArray.map(img => { return imgArray.map(img => {
if (!img.src.includes('brokenfile')) { if (!img.src.includes('brokenfile')) {
@ -337,18 +334,23 @@
const questions = readQuestions.map(question => { const questions = readQuestions.map(question => {
return { return {
Q: question.question, Q: question.question,
possibleAnswers: question.possibleAnswers,
data: question.data, data: question.data,
} }
}) })
post('ask', { const sentData = {
questions: questions, questions: questions,
subj: getCurrentSubjectName(), subj: getCurrentSubjectName(),
location: currUrl, testUrl: currUrl,
version: info().script.version, version: info().script.version,
cid: cid, cid: getCid(),
uid: uid, uid: getUid(),
}).then(results => { }
console.log(sentData)
post('ask', sentData).then(results => {
removeLoadingMessage() removeLoadingMessage()
ShowAnswers( ShowAnswers(
results.map((res, i) => { results.map((res, i) => {
@ -502,11 +504,7 @@
...question, ...question,
...possibleAnswerArray, ...possibleAnswerArray,
]) ])
const data = getDataFromTest( const data = getDataFromTest(images, getLegacyImageID(imageNodes))
images,
possibleAnswers,
getLegacyImageID(imageNodes)
)
resolve({ resolve({
question: questionText, question: questionText,
@ -529,11 +527,10 @@
}) })
} }
function getDataFromTest(hashedImages, possibleAnswers, legacyImages) { function getDataFromTest(hashedImages, legacyImages) {
if (hashedImages.length > 0) { if (hashedImages.length > 0) {
return { return {
type: 'image', type: 'image',
possibleAnswers,
hashedImages: hashedImages.map(x => { hashedImages: hashedImages.map(x => {
return x.val return x.val
}), }),
@ -542,7 +539,6 @@
} else { } else {
return { return {
type: 'simple', type: 'simple',
possibleAnswers,
} }
} }
} }
@ -1031,7 +1027,7 @@
try { try {
const idInput = document.getElementById('cid') const idInput = document.getElementById('cid')
if (idInput) { if (idInput) {
idInput.value = getVal('clientId') idInput.value = getCid()
} }
} catch (e) { } catch (e) {
console.info('Error filling client ID input', e) console.info('Error filling client ID input', e)
@ -1064,7 +1060,7 @@
const cidSetInterval = setInterval(() => { const cidSetInterval = setInterval(() => {
const cid = document.getElementById('cid') const cid = document.getElementById('cid')
if (cid) { if (cid) {
cid.value = GetId() + '|' + info().script.version cid.value = getCid() + '|' + info().script.version
window.clearInterval(cidSetInterval) window.clearInterval(cidSetInterval)
} }
}, 100) }, 100)
@ -1148,11 +1144,10 @@
motd = inf.motd motd = inf.motd
userSpecificMotd = inf.userSpecificMotd userSpecificMotd = inf.userSpecificMotd
subjInfo = inf.subjinfo subjInfo = inf.subjinfo
uid = inf.uid setVal('userId', inf.uid)
cid = getVal('clientId') overlay.querySelector('#infoMainDiv').innerText = `${
overlay.querySelector( subjInfo.subjects
'#infoMainDiv' } tárgy, ${subjInfo.questions} kérdés. User ID: ${getUid()}`
).innerText = `${subjInfo.subjects} tárgy, ${subjInfo.questions} kérdés. User ID: ${uid}`
// FIXME: if cwith() throws an unhandled error it sais server is not avaible // FIXME: if cwith() throws an unhandled error it sais server is not avaible
cwith() cwith()
}) })
@ -1167,8 +1162,8 @@
}) })
SafeGetElementById('infoMainDiv', elem => { SafeGetElementById('infoMainDiv', elem => {
elem.innerText = texts.noUser elem.innerText = texts.noUser
if (getVal('clientId')) { if (getCid()) {
elem.innerText += ` (${getVal('clientId')})` elem.innerText += ` (${getCid()})`
} }
}) })
SafeGetElementById('loginDiv', elem => { SafeGetElementById('loginDiv', elem => {
@ -1398,7 +1393,7 @@
try { try {
sentData = { sentData = {
version: info().script.version, version: info().script.version,
id: GetId(), id: getCid(),
quiz: quiz, quiz: quiz,
location: currUrl, location: currUrl,
} }
@ -1439,8 +1434,8 @@
var firstRun = getVal('firstRun') // if the current run is the frst var firstRun = getVal('firstRun') // if the current run is the frst
if (firstRun === undefined || firstRun === true) { if (firstRun === undefined || firstRun === true) {
setVal('firstRun', false) setVal('firstRun', false)
// TODO: add install source to query. Maybe even a new api endpoint to register installion
ShowHelp('firstRun') // showing help ShowHelp('firstRun') // showing help
registerScript()
document.write(texts.freshStartWarning) document.write(texts.freshStartWarning)
document.close() document.close()
@ -1448,6 +1443,36 @@
} }
} }
function registerScript() {
try {
// setVal('registeredWithCid', false)
// setVal('registeredWithUid', true)
if (getVal('registeredWithCid')) {
if (getVal('registeredWithUid')) {
return
} else if (!getUid()) {
return
}
}
setVal('registeredWithCid', true)
if (getUid()) {
setVal('registeredWithUid', true)
}
post('registerscript', {
cid: getCid(),
uid: getUid(),
version: info().script.version,
date: new Date(),
})
} catch (err) {
console.warn('Unexpected error while registering script')
console.log(err)
}
}
// : }}} // : }}}
// : Video hotkey stuff {{{ // : Video hotkey stuff {{{
@ -2241,7 +2266,11 @@
log('------------------------------------------') log('------------------------------------------')
} }
function GetId() { function getUid() {
return getVal('userId')
}
function getCid() {
let currId = getVal('clientId') let currId = getVal('clientId')
if (currId) { if (currId) {
return currId return currId
@ -2284,6 +2313,7 @@
} }
function GetXHRInfos() { function GetXHRInfos() {
registerScript()
const now = new Date().getTime() const now = new Date().getTime()
const lastCheck = getVal('lastInfoCheckTime') const lastCheck = getVal('lastInfoCheckTime')
if (!lastCheck) { if (!lastCheck) {
@ -2308,7 +2338,7 @@
'infos?version=true&motd=true&subjinfo=true&cversion=' + 'infos?version=true&motd=true&subjinfo=true&cversion=' +
info().script.version + info().script.version +
'&cid=' + '&cid=' +
GetId() getCid()
xmlhttpRequest({ xmlhttpRequest({
method: 'GET', method: 'GET',
@ -2323,6 +2353,9 @@
setVal('lastInfoCheckTime', now) setVal('lastInfoCheckTime', now)
const res = JSON.parse(response.responseText) const res = JSON.parse(response.responseText)
setVal('lastInfo', response.responseText) setVal('lastInfo', response.responseText)
if (getUid() !== res.uid) {
setVal('userId', res.uid)
}
resolve(res) resolve(res)
} catch (e) { } catch (e) {
log('Errro paring JSON in GetXHRInfos') log('Errro paring JSON in GetXHRInfos')
@ -2367,7 +2400,7 @@
'&cversion=' + '&cversion=' +
info().script.version + info().script.version +
'&cid=' + '&cid=' +
GetId() getCid()
xmlhttpRequest({ xmlhttpRequest({
method: 'GET', method: 'GET',
@ -2433,8 +2466,8 @@
} }
}) })
queries.push('version=' + encodeURIComponent(info().script.version)) queries.push('version=' + encodeURIComponent(info().script.version))
queries.push('uid=' + encodeURIComponent(uid)) queries.push('uid=' + encodeURIComponent(getUid()))
queries.push('cid=' + encodeURIComponent(cid)) queries.push('cid=' + encodeURIComponent(getCid()))
} catch (e) { } catch (e) {
Exception(e, 'error at setting error stack/msg link') Exception(e, 'error at setting error stack/msg link')
} }