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