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==
// @name Moodle/Elearning/KMOOC test help
// @version 2.0.0.4
// @version 2.0.0.5
// @description Online Moodle/Elearning/KMOOC test help
// @author MrFry
// @match https://elearning.uni-obuda.hu/main/*
@ -73,6 +73,7 @@
const log = true
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 lastestVersion = ''
var subjInfo
@ -1613,6 +1614,15 @@
}
function GetXHRInfos () {
const now = new Date().getTime()
const lastCheck = getVal('lastInfoCheckTime')
if (!lastCheck) {
setVal('lastInfoCheckTime', now)
}
if (now > lastCheck + (infoExpireTime * 1000)) {
console.log('GETTING DATA FROM SREVER')
setVal('lastInfoCheckTime', now)
return new Promise((resolve, reject) => {
xmlhttpRequest({
method: 'GET',
@ -1620,6 +1630,7 @@
onload: function (response) {
try {
const res = JSON.parse(response.responseText)
setVal('lastInfo', response.responseText)
resolve(res)
} catch (e) {
Log('Errro paring JSON in GetXHRInfos')
@ -1633,6 +1644,18 @@
}
})
})
} 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)
}
})
}
}
function GetXHRQuestionAnswer (question) {