Last active
June 6, 2020 06:38
-
-
Save ahallrq/2c0fe9d6d348d2987e1b5bced39b497c to your computer and use it in GitHub Desktop.
A Tampermonkey script to automatically reload the Victron Venus Web Console on disconnect.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Auto Reload Console | |
// @namespace https://redquasar.xyz | |
// @version 0.1 | |
// @description Auto Reload Console | |
// @author Andrew Hall | |
// @match http://192.168.1.104/* | |
// @grant GM_addStyle | |
// ==/UserScript== | |
//var dialogHTML = '<dialog id="reconnectDialog">' | |
var dialogHTML = '<div id="reconnectDialogHeader">Disconnected</div>' | |
dialogHTML += ' <div id="reconnectDialogInner">' | |
dialogHTML += ' <label>Reconnecting in: </label><label id="timerVal">10</label>' | |
dialogHTML += ' <div><progress id="timerProgress" value=1></progress></div>' | |
dialogHTML += ' <div><button id="timerStopBtn">Cancel</button>' | |
dialogHTML += ' </div>' | |
dialogHTML += ' </div>' | |
//dialogHTML += '</dialog>' | |
GM_addStyle(`#reconnectDialog { | |
border: 2px solid grey; | |
border-radius: 5px; | |
padding: 5px; | |
text-align: center; | |
/*font-family: Helvetica Neue,Helvetica,Arial,sans-serif;*/ | |
background-color: #cccccc; | |
width: 250px; | |
position: absolute; | |
top: 50%; | |
transform: translate(0%, -50%); | |
} | |
#reconnectDialogHeader { | |
text-align: center; | |
padding-bottom: 5px; | |
border-bottom:1px solid grey; | |
font-size: 1.25em; | |
font-weight: bold; | |
} | |
#reconnectDialogInner { | |
text-align: center; | |
margin-left: auto; | |
margin-right: auto; | |
padding: 5px; | |
} | |
#timerStopBtn { | |
font-size: 1.2em; | |
padding-left: 8px; | |
padding-right: 8px; | |
} | |
#timerProgress { | |
width: 100%; | |
} | |
`); | |
var consoleTimer; | |
var progressTimer; | |
var dialogEvent; | |
var dialogBtnEvent; | |
var reloadTime = 10; | |
var autoreload = true; | |
function consoleTimerCb() { | |
console.log("consoleTimerCb loop"); | |
if (disconnected && autoreload) { | |
clearInterval(consoleTimer); | |
var dlg = document.getElementById("reconnectDialog"); | |
dlg.showModal(); | |
dialogEventCb(); | |
} else { | |
autoreload = true; | |
} | |
} | |
function dialogBtnEventCb() { | |
clearTimeout(progressTimer); | |
//clearTimeout(consoleTimer); | |
reloadTime = 10; | |
document.getElementById("reconnectDialog").close(); | |
autoreload = false; | |
consoleTimer = setInterval(consoleTimerCb, 1000); | |
} | |
function dialogEventCb() { | |
progressTimer = setInterval(function() { | |
if (reloadTime < 1) { | |
window.location.reload(); | |
} | |
document.getElementById("timerVal").textContent = reloadTime; | |
document.getElementById("timerProgress").value = reloadTime/10; | |
reloadTime -= 1; | |
}, 1000); | |
} | |
(function() { | |
'use strict'; | |
var dialogElem = document.createElement('dialog'); | |
dialogElem.id = "reconnectDialog"; | |
dialogElem.innerHTML = dialogHTML; | |
document.body.appendChild(dialogElem); | |
consoleTimer = setInterval(consoleTimerCb, 1000); | |
//dialogEvent = document.getElementById("reconnectDialog").addEventListener("dialogopen", dialogEventCb); | |
dialogBtnEvent = document.getElementById("timerStopBtn").addEventListener("click", dialogBtnEventCb); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment