Info getting only every x seconds

This commit is contained in:
MrFry 2020-03-31 15:37:14 +02:00
parent 2f9446157b
commit bf2b44ee56

View file

@ -21,7 +21,7 @@
// ==UserScript== // ==UserScript==
// @name Moodle/Elearning/KMOOC test help // @name Moodle/Elearning/KMOOC test help
// @version 2.0.0.4 // @version 2.0.0.5
// @description Online Moodle/Elearning/KMOOC test help // @description Online Moodle/Elearning/KMOOC test help
// @author MrFry // @author MrFry
// @match https://elearning.uni-obuda.hu/main/* // @match https://elearning.uni-obuda.hu/main/*
@ -73,6 +73,7 @@
const log = true const log = true
const motdShowCount = 3 /* Ammount of times to show motd */ const motdShowCount = 3 /* Ammount of times to show motd */
const infoExpireTime = 60 // Every n seconds basic info should be loaded from server
var motd = '' var motd = ''
var lastestVersion = '' var lastestVersion = ''
var subjInfo var subjInfo
@ -1613,26 +1614,48 @@
} }
function GetXHRInfos () { function GetXHRInfos () {
return new Promise((resolve, reject) => { const now = new Date().getTime()
xmlhttpRequest({ const lastCheck = getVal('lastInfoCheckTime')
method: 'GET', if (!lastCheck) {
url: apiAdress + 'infos?version=true&motd=true&subjinfo=true&cversion=' + info().script.version, setVal('lastInfoCheckTime', now)
onload: function (response) { }
try {
const res = JSON.parse(response.responseText) if (now > lastCheck + (infoExpireTime * 1000)) {
resolve(res) console.log('GETTING DATA FROM SREVER')
} catch (e) { setVal('lastInfoCheckTime', now)
Log('Errro paring JSON in GetXHRInfos') return new Promise((resolve, reject) => {
Log(e) xmlhttpRequest({
method: 'GET',
url: apiAdress + 'infos?version=true&motd=true&subjinfo=true&cversion=' + info().script.version,
onload: function (response) {
try {
const res = JSON.parse(response.responseText)
setVal('lastInfo', response.responseText)
resolve(res)
} catch (e) {
Log('Errro paring JSON in GetXHRInfos')
Log(e)
reject(e)
}
},
onerror: (e) => {
Log('Info get Error', e)
reject(e) reject(e)
} }
}, })
onerror: (e) => { })
Log('Info get Error', e) } else {
console.log('USING OLD')
return new Promise((resolve, reject) => {
try {
resolve(JSON.parse(getVal('lastInfo')))
} catch (e) {
Log('Errro paring JSON in GetXHRInfos, when using old data!')
Log(e)
reject(e) reject(e)
} }
}) })
}) }
} }
function GetXHRQuestionAnswer (question) { function GetXHRQuestionAnswer (question) {