peer selector improvements

This commit is contained in:
mrfry 2023-04-10 10:59:30 +02:00
parent b46180c25a
commit d9e5c8bdb0

73
user.js
View file

@ -1749,7 +1749,9 @@
clearAllMessages() clearAllMessages()
SafeGetElementById('peerSelector', (elem) => { SafeGetElementById('peerSelector', (elem) => {
elem.style.display = 'none' elem.style.display = 'none'
updatePeerSelector(elem)
}) })
GetXHRInfos() GetXHRInfos()
.then((inf) => { .then((inf) => {
try { try {
@ -1789,15 +1791,7 @@
}) })
if (skipAvailablePeerFind || !getVal('lastp2pinfo')) { if (skipAvailablePeerFind || !getVal('lastp2pinfo')) {
SafeGetElementById('retryContainer', (elem) => { connectionErrorAction()
elem.style.display = 'flex'
})
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = ''
})
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
} else { } else {
tryAnotherPeer() tryAnotherPeer()
} }
@ -1855,33 +1849,11 @@
) )
connectToPeer(suitablePeer) connectToPeer(suitablePeer)
} else { } else {
SafeGetElementById('retryContainer', (elem) => { connectionErrorAction(texts.noPeersOnline)
elem.style.display = 'flex'
})
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = ''
})
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = texts.noPeersOnline
})
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
debugLog('None of the peers are online!') debugLog('None of the peers are online!')
} }
} catch (e) { } catch (e) {
SafeGetElementById('retryContainer', (elem) => { connectionErrorAction(texts.peerTryingError)
elem.style.display = 'flex'
})
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = ''
})
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = texts.peerTryingError
})
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
warn('Error ocurred during trying to connect to peers!') warn('Error ocurred during trying to connect to peers!')
warn(e) warn(e)
} }
@ -1909,6 +1881,21 @@
ConnectToServer() ConnectToServer()
} }
function connectionErrorAction(infoText) {
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = infoText || texts.noServer
})
SafeGetElementById('retryContainer', (elem) => {
elem.style.display = 'flex'
})
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = ''
})
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
}
function NoUserAction() { function NoUserAction() {
SafeGetElementById('scriptMenuDiv', (elem) => { SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = '#44cc00' elem.style.backgroundColor = '#44cc00'
@ -2817,13 +2804,7 @@
resetMenu() resetMenu()
}) })
try { updatePeerSelector(peerSelector)
const selectedPeer = JSON.parse(getVal('peerToUse'))
const selectedIndex = peers.findIndex((x) => {
return getPeerUrl(x) === getPeerUrl(selectedPeer)
})
peerSelector.value = selectedIndex
} catch (e) {}
return peerSelector return peerSelector
} catch (e) { } catch (e) {
@ -2904,6 +2885,18 @@
} }
} }
function updatePeerSelector(selector) {
try {
const peers = [originalServer, ...JSON.parse(getVal('lastp2pinfo'))]
const selectedPeer = JSON.parse(getVal('peerToUse'))
const selectedIndex = peers.findIndex((x) => {
return getPeerUrl(x) === getPeerUrl(selectedPeer)
})
selector.value = selectedIndex
} catch (e) {}
}
// : }}} // : }}}
// : Generic utils {{{ // : Generic utils {{{