This commit is contained in:
parent
428b042fe9
commit
dbe74f4ed1
12
index.js
12
index.js
|
@ -1,4 +1,4 @@
|
|||
const storageType = 'sessionStorage'
|
||||
const storageType = 'localStorage'
|
||||
|
||||
var data = {}
|
||||
|
||||
|
@ -26,7 +26,7 @@ function locallyGetInstances () {
|
|||
const item = window[storageType].getItem('instances')
|
||||
try {
|
||||
return JSON.parse(item)
|
||||
} finally {
|
||||
} catch (_err) {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ async function remotelyGetInstances () {
|
|||
.then((json) => Object.entries(json.instances))
|
||||
.then((entries) => entries.filter(([_url, details]) => {
|
||||
if (details.network_type !== 'normal') return
|
||||
if (details.uptime == null) return
|
||||
if (details.uptime.uptimeDay !== 100) return
|
||||
return true
|
||||
}))
|
||||
|
@ -55,15 +56,22 @@ async function remotelyGetInstances () {
|
|||
console.error(err)
|
||||
return []
|
||||
})
|
||||
window[storageType].setItem('timestamp', Date.now().toString())
|
||||
return instances
|
||||
}
|
||||
|
||||
// Get local instances
|
||||
data.instances = locallyGetInstances()
|
||||
|
||||
// Fetch remote instances if necessary
|
||||
const timestamp = +window[storageType].getItem('timestamp')
|
||||
if (Date.now() > timestamp + 3_600_000 /* 1 hour */) {
|
||||
const remoteInstances = await remotelyGetInstances()
|
||||
if (remoteInstances.length > 0) {
|
||||
data.instances = remoteInstances
|
||||
locallySetInstances()
|
||||
}
|
||||
}
|
||||
|
||||
if (data.instances.length > 0) {
|
||||
infoEl.textContent = `${data.instances.length} instances`
|
||||
|
|
Loading…
Reference in New Issue
Block a user