diff --git a/content/easy-ssh-tunnel/index.html b/content/easy-ssh-tunnel/index.html index a8077cfe..ce477508 100644 --- a/content/easy-ssh-tunnel/index.html +++ b/content/easy-ssh-tunnel/index.html @@ -8,17 +8,16 @@
@@ -92,14 +91,15 @@ - + - + + @@ -109,19 +109,31 @@ const remoteHost = document.querySelector('#remotehost'); const direction = document.querySelector('#direction'); const command = document.querySelector('#command'); + const copy = document.querySelector('#copy'); + const valueOrPlaceholder = (v) => v.value || v.placeholder; function updateCommand() { - const localPortValue = localPort.value; - const remotePortValue = remotePort.value; const directionString = direction.checked ? "R" : "L"; - const remoteHostValue = remoteHost.value || remoteHost.placeholder; - command.textContent = `ssh -f -N -${directionString} ${localPortValue}:localhost:${remotePortValue} ${remoteHostValue}`; + command.textContent = `ssh -f -N -${directionString} ${valueOrPlaceholder(localPort)}:localhost:${valueOrPlaceholder(remotePort)} ${valueOrPlaceholder(remoteHost)}`; } - localPort.addEventListener('input', updateCommand); - direction.addEventListener('change', updateCommand); - remotePort.addEventListener('input', updateCommand); - remoteHost.addEventListener('input', updateCommand); + function validatePort(e) { + const code = e.keyCode; + if (code < 48 || code > 57 || e.target.value * 10 + (code - 48) > 65535) { + e.preventDefault() + } + } + + for (const variable of [localPort, direction, remoteHost, remotePort]) { + variable.addEventListener('input', updateCommand); + } + + for (const portVariable of [localPort, remotePort]) { + portVariable.addEventListener('keypress', validatePort); + } + + copy.addEventListener('click', (_) => navigator.clipboard.writeText(command.textContent)) + updateCommand()