Onionr/static-data/www/shared/sidebar/sidebar.js

167 lines
5.7 KiB
JavaScript
Raw Normal View History

2020-08-05 03:35:45 +00:00
fetch('/shared/sidebar/sidebar.html', {
"method": "get",
headers: {
"token": webpass
}})
.then((resp) => resp.text())
.then(function(resp) {
document.getElementById('sidebarContainer').innerHTML = resp
2020-08-21 14:57:59 +00:00
var quickviews = bulmaQuickview.attach()
sidebarAddPeerRegister()
2020-08-05 03:35:45 +00:00
})
2020-10-17 10:11:07 +00:00
var sidebarActive = false
2020-10-17 09:54:29 +00:00
var lastLogOffset = 0
2020-10-17 10:11:07 +00:00
var logActive = false
var maxLogOutputSize = 1000000
var logfileOutputEl = null
2020-10-17 10:11:07 +00:00
2020-10-17 09:54:29 +00:00
async function showLog(){
fetch('/readfileoffset/onionr.log?offset=' + lastLogOffset, {
method: 'GET',
headers: {
"token": webpass
}})
.then((resp) => resp.json())
.then(function(resp){
let doScroll = function(){
if (!logActive){
logfileOutputEl.scrollTop = logfileOutputEl.scrollHeight;
}
}
if (! resp.data){
doScroll()
return
}
2020-10-17 09:54:29 +00:00
lastLogOffset = resp['new_offset']
var length = (new TextEncoder().encode(logfileOutputEl.innerText)).length
var tempText = logfileOutputEl.innerText
while(length > maxLogOutputSize){
tempText = tempText.substring(tempText.indexOf("\n") + 1)
length = (new TextEncoder().encode(tempText)).length
logfileOutputEl.innerText = tempText
}
2020-10-17 10:11:07 +00:00
logfileOutputEl.innerText += resp.data.replace(
2020-10-17 09:54:29 +00:00
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '')
2020-10-22 12:45:38 +00:00
logfileOutputEl.innerText += "\n\n"
doScroll()
2020-10-17 09:54:29 +00:00
})
}
function sidebarAddPeerRegister(){
document.getElementById('addPeerBtn').onclick = function(){
let newPeer = document.getElementById('addPeerInput').value
if (! newPeer.includes(".")){
PNotify.error({text: "Invalid peer address"})
return
}
fetch('/addpeer/' + newPeer, {
method: 'POST',
headers: {
"token": webpass
}})
.then(function(resp){
if (! resp.ok){
2020-10-15 23:24:25 +00:00
if (resp.status == 409){
PNotify.notice({text: "Peer already added"})
throw new Error("Could not add peer " + newPeer + " already added")
2020-10-15 23:24:25 +00:00
}
PNotify.error({text: "Could not add peer. Is your input valid?"})
throw new Error("Could not add peer " + newPeer)
}
return resp
})
.then((resp) => resp.text())
.then(function(data) {
if (data == "success"){
PNotify.success({
text: 'Peer added'
})
return
}
else if(data == "already added"){
PNotify.notice({
text: 'Peer already added'
})
return
}
PNotify.error({text: data})
})
}
}
2020-08-05 03:35:45 +00:00
window.addEventListener("keydown", function(event) {
2020-10-17 09:54:29 +00:00
var refreshSideBarInterval = null
2020-10-17 10:11:07 +00:00
document.getElementById('logfileOutput').onmouseenter = function(e){
logActive = true
}
document.getElementById('logfileOutput').onmouseleave = function(e){
logActive = false
}
2020-10-17 09:54:29 +00:00
document.getElementsByClassName('closeSidebar')[0].onclick = function(){
2020-10-17 10:11:07 +00:00
sidebarActive = false
2020-10-17 09:54:29 +00:00
clearInterval(sidebarLogInterval)
clearInterval(refreshSideBarInterval)
}
2020-08-05 03:35:45 +00:00
if (event.key === "s"){
logfileOutputEl = document.getElementById('logfileOutput')
2020-10-17 10:11:07 +00:00
sidebarActive = true
2020-08-21 14:57:59 +00:00
if (document.activeElement.nodeName == "TEXTAREA" || document.activeElement.nodeName == "INPUT"){
if (! document.activeElement.hasAttribute("readonly")){
return
}
}
2020-10-17 09:54:29 +00:00
sidebarLogInterval = setInterval(function(){showLog()}, 1000)
2020-08-21 14:57:59 +00:00
let refreshSideBar = function(){
if (document.hidden){return}
var existingValue = document.getElementById("insertingBlocks").innerText
var existingUploadValue = document.getElementById("uploadBlocks")
2020-08-21 14:57:59 +00:00
fetch('/getgeneratingblocks', {
"method": "get",
headers: {
"token": webpass
}})
.then((resp) => resp.text())
.then(function(resp) {
console.debug(resp.length, existingValue)
if (resp.length <= 2 && existingValue !== "0"){
document.getElementById("insertingBlocks").innerText = "0"
return
}
if (existingValue === resp.split(',').length){
return
}
document.getElementById("insertingBlocks").innerText = resp.split(',').length - 1
})
fetch('/getblockstoupload', {
"method": "get",
headers: {
"token": webpass
}})
.then((resp) => resp.text())
.then(function(resp) {
if (resp.length <= 2 && existingUploadValue !== "0"){
document.getElementById("uploadBlocks").innerText = "0"
return
}
if (existingUploadValue === resp.split(',').length){
return
}
document.getElementById("uploadBlocks").innerText = resp.split(',').length - 1
})
2020-08-21 14:57:59 +00:00
}
2020-10-17 09:54:29 +00:00
refreshSideBarInterval = setInterval(refreshSideBar, 3000)
2020-08-21 14:57:59 +00:00
setTimeout(function(){document.getElementsByClassName('sidebarBtn')[0].click()}, 300)
2020-08-05 03:35:45 +00:00
}
}, true)