2021-07-20 13:16:17 -07:00
|
|
|
/* Copyright (C) 2021 Samuel L Sloniker KJ7RRV
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-17 14:33:40 -07:00
|
|
|
function resize() {
|
|
|
|
let bodyWidth = window.innerWidth
|
|
|
|
let maxWidth = Math.min(bodyWidth - 60, 800)
|
|
|
|
let bodyHeight = window.innerHeight
|
|
|
|
let maxHeight = Math.min(bodyHeight - 60, 480)
|
|
|
|
let equivalentHeight = maxWidth * 0.6
|
|
|
|
if (equivalentHeight >= maxHeight) {
|
|
|
|
displayHeight = Math.round(maxHeight)
|
|
|
|
displayWidth = Math.round(maxHeight * (5/3))
|
|
|
|
} else {
|
|
|
|
displayWidth = Math.round(maxWidth)
|
|
|
|
displayHeight = Math.round(maxWidth * 0.6)
|
|
|
|
}
|
2021-07-19 12:09:42 -07:00
|
|
|
img.style.width = displayWidth + 'px'
|
|
|
|
img.style.height = displayHeight + 'px'
|
2021-06-17 14:33:40 -07:00
|
|
|
errorbox.style.width = displayWidth + 'px'
|
|
|
|
errorbox.style.height = displayHeight + 'px'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setup() {
|
|
|
|
ws = new WebSocket(localStorage.getItem('url'))
|
|
|
|
ws.onmessage = function(e) {
|
|
|
|
window.m = e
|
2021-07-19 12:09:42 -07:00
|
|
|
let type, data, code, msg
|
2021-06-17 14:33:40 -07:00
|
|
|
type = e.data.split('%')[0]
|
2021-07-05 11:39:56 -07:00
|
|
|
if (type == 'ver') {
|
|
|
|
ws.send('pass ' + localStorage.getItem('password'))
|
2021-07-10 14:28:36 -07:00
|
|
|
setInterval(function(){ws.send('ack')}, 3000)
|
2021-07-05 11:39:56 -07:00
|
|
|
} else if ( type == 'pic' ) {
|
2021-06-17 14:33:40 -07:00
|
|
|
data = e.data.split('%')[2]
|
|
|
|
img.src = data
|
|
|
|
errorbox.style.display = 'none'
|
2021-07-19 12:09:42 -07:00
|
|
|
img.style.display = 'block'
|
2021-06-17 14:33:40 -07:00
|
|
|
} else if ( type == 'err' ) {
|
|
|
|
code = e.data.split('%')[1]
|
|
|
|
msg = e.data.split('%')[2]
|
|
|
|
errorbox.textContent = msg
|
|
|
|
errorbox.style.display = 'block'
|
2021-07-19 12:09:42 -07:00
|
|
|
img.style.display = 'none'
|
2021-06-17 14:33:40 -07:00
|
|
|
console.error('Server reported error: ' + code + ': ' + msg)
|
|
|
|
if (code[0] == '*') {
|
|
|
|
errorbox.innerHTML = msg + '<br>Please refresh page'
|
|
|
|
ws.onclose = function(e){}
|
|
|
|
ws.onerror = function(e){}
|
|
|
|
}
|
|
|
|
} else if (type == 'ack') {
|
|
|
|
ws.send('ack')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.onerror = function(e){
|
|
|
|
errorbox.innerHTML = 'Connection lost<br>Please refresh page'
|
|
|
|
errorbox.style.display = 'block'
|
2021-07-19 12:09:42 -07:00
|
|
|
img.style.display = 'none'
|
2021-06-17 14:33:40 -07:00
|
|
|
}
|
|
|
|
ws.onclose = function(e){
|
|
|
|
errorbox.innerHTML = 'Connection lost<br>Please refresh page'
|
|
|
|
errorbox.style.display = 'block'
|
2021-07-19 12:09:42 -07:00
|
|
|
img.style.display = 'none'
|
2021-06-17 14:33:40 -07:00
|
|
|
}
|
2021-07-05 11:21:10 -07:00
|
|
|
ws.onopen = function(e) {
|
2021-07-05 11:39:56 -07:00
|
|
|
ws.send('maxver 1')
|
2021-07-05 11:21:10 -07:00
|
|
|
}
|
2021-06-17 14:33:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function touch(e) {
|
|
|
|
click_timeout = setTimeout(function() {
|
|
|
|
is_short = false
|
|
|
|
}, 1000)
|
2021-06-17 14:52:23 -07:00
|
|
|
is_short = true
|
2021-06-17 14:33:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function release(e) {
|
2021-07-19 12:09:42 -07:00
|
|
|
x = e.offsetX || e.layerX
|
|
|
|
y = e.offsetY || e.layerY
|
2021-06-17 14:33:40 -07:00
|
|
|
w = displayWidth
|
2021-06-17 14:52:23 -07:00
|
|
|
length = is_short?false:true
|
2021-07-05 11:21:10 -07:00
|
|
|
ws.send('touch ' + x + ' ' + y + ' ' + w + ' ' + length)
|
2021-06-17 14:52:23 -07:00
|
|
|
clearTimeout(click_timeout)
|
|
|
|
is_short = true
|
2021-06-17 14:33:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function connect(e) {
|
|
|
|
e.preventDefault()
|
|
|
|
holder.style.display = 'block'
|
|
|
|
login.style.display = 'none'
|
|
|
|
localStorage.setItem('url', url_el.value)
|
|
|
|
localStorage.setItem('password', password_el.value)
|
|
|
|
setup()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-19 12:09:42 -07:00
|
|
|
let img = document.querySelector('#img')
|
2021-06-17 14:33:40 -07:00
|
|
|
let errorbox = document.querySelector('#errorbox')
|
|
|
|
|
|
|
|
let login = document.querySelector('#login')
|
|
|
|
let holder = document.querySelector('#holder')
|
|
|
|
|
|
|
|
let url_el = document.querySelector('#url')
|
|
|
|
let password_el = document.querySelector('#password')
|
|
|
|
let start_button = document.querySelector('#start')
|
|
|
|
|
|
|
|
let is_short = true
|
|
|
|
let click_timeout
|
|
|
|
|
|
|
|
let displayWidth, displayHeight
|
|
|
|
|
|
|
|
let ws
|
|
|
|
|
|
|
|
|
|
|
|
resize()
|
|
|
|
window.onresize = resize
|
|
|
|
|
2021-07-19 12:09:42 -07:00
|
|
|
img.onmousedown = touch
|
|
|
|
img.onmouseup = release
|
2021-06-17 14:33:40 -07:00
|
|
|
|
|
|
|
start.onclick = connect
|