From e1e666a750ad44e1a8624b6bc6864fb505332043 Mon Sep 17 00:00:00 2001 From: theredkoala21 <37466255+theredkoala21@users.noreply.github.com> Date: Thu, 4 Apr 2019 14:10:47 +1100 Subject: [PATCH 1/3] Auto Connect / Auto Login + logo & version display + added ability to auto connect and auto login ( self ) + add logo ( https://github.com/micropython/webrepl/commit/99a568dc251ff0151e327e097ca43ae4e53a5fdd ) + add version display on connect ( https://github.com/micropython/webrepl/commit/8e59ff0c49a581894ad57a0b62cf371ddecc81a8 ) --- webrepl.html | 93 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 6 deletions(-) diff --git a/webrepl.html b/webrepl.html index 297d97e..f4c6adf 100644 --- a/webrepl.html +++ b/webrepl.html @@ -16,7 +16,11 @@ margin-bottom: 20px; font: 20px/1.5 sans-serif; } - + .logo { + position: absolute; + bottom: 10px; + right: 10px; + } /* .terminal { float: left; @@ -48,6 +52,7 @@
+
@@ -70,11 +75,21 @@
(file operation status)
+ +
Terminal widget should be focused (text cursor visible) to accept input. Click on it if not.
To paste, press Ctrl+A, then Ctrl+V + + + @@ -77,6 +98,10 @@ +
Saved Sessions:

+ +
+
@@ -104,6 +129,8 @@ var get_file_name = null; var get_file_data = null; var version = null; +var jurl = null; +var sessionList = null; function calculate_size(win) { var cols = Math.max(80, Math.min(150, (win.innerWidth - 280) / 7)) | 0; @@ -111,25 +138,106 @@ return [cols, rows]; } -var jurl = null; function buildLink() { - var j = {"url":"","pass":"","autoconnect":"True"}; + var j = {"url":"","pass":"","autoconnect":true}; j.url = prompt("Enter URL:", document.getElementById('url').value); j.pass = prompt("Enter Password:"); j.autoconnect = confirm("Enable AutoConnect?"); - window.location.hash = JSON.stringify(j); +} + +// localStorage.setItem("lastname", "Smith"); +function addSession() { +if (typeof(Storage) === "undefined") { + console.error("[ERROR] No localStorage"); + return; +} +var j = {"url":"","pass":"","autoconnect":true}; +j.url = prompt("Enter URL:", document.getElementById('url').value); +j.pass = prompt("Enter Password:"); +j.autoconnect = confirm("Enable AutoConnect?"); +if(j.url == null){ + alert('[ERROR] URL is empty!'); + return; +} + +loadSessions(); + +for (i in sessionList) { + var u = sessionList[i].url; + if(u == j.url) { + alert('Already exists!'); + return; + } +} +sessionList.push(j); +localStorage.setItem('sessionList', JSON.stringify(sessionList)); + +loadSessionList(); +} + +function resetSessons() { + localStorage.setItem('sessionList', ''); + sessionList = null; + document.getElementById('saved-sessions').innerHTML = ''; +} + +function loadSessions() { +if (typeof(Storage) === "undefined") { + console.error("[ERROR] No localStorage"); + return; +} +try { + sessionList = JSON.parse(localStorage.getItem('sessionList')); +} catch(error) { + console.error(error); + if(typeof sessionList === 'undefined' || sessionList === null) { + sessionList = []; + localStorage.setItem('sessionList', JSON.stringify(sessionList)); + return; + } + return; +} +return; +} + +function loadSessionList() { + var s = document.getElementById('saved-sessions'); + s.innerHTML = ""; + + for (var i in sessionList) { + var tu = sessionList[i].url.replace('ws://',''); + s.innerHTML = s.innerHTML + "
  • " + tu + "
  • "; + } +} + +function connectSession(e) { + console.log('Connecting to: ' + sessionList[e.lang].url); + var s = sessionList[e.lang]; + + + jurl = {"url":s.url,"pass":s.pass,"autoconnect":s.autoconnect}; + document.getElementById('url').disabled = true; + document.getElementById('button').value = "Disconnect"; + connected = true; + document.getElementById('url').value = jurl.url; + connect(jurl.url); +} + +function deleteSession(e) { + sessionList.splice(e.lang, 1); + localStorage.setItem('sessionList', JSON.stringify(sessionList)); + loadSessionList(); } (function() { window.onload = function() { try { jurl = JSON.parse(decodeURI(window.location.hash.substring(1))); - console.log(jurl); if(jurl.url) { document.getElementById('url').value = jurl.url; @@ -137,10 +245,9 @@ setTimeout(function() { button_click(); }, 100); } } - } catch(error) { - console.log(error); + console.error(error); console.log("Attempting Alt Method of URL parsing."); var url = window.location.hash.substring(1); if (url) { @@ -149,8 +256,9 @@ } + loadSessions(); + loadSessionList(); - var size = calculate_size(self); term = new Terminal({ cols: size[0], @@ -327,22 +435,26 @@ } function managePasswordReq(data) { - if(typeof data == 'string' && data.indexOf('Password:') > -1 && jsonExists(jurl, 'pass')) { + if(typeof data == 'string' && data.indexOf('Password:') > -1 && jsonKeyExists(jurl, 'pass')) { term.write('\x1b[36mAttempting AutoLogin\x1b[m\r\n'); term.send(jurl.pass + "\r"); } } -function jsonExists(pri, key) { +function jsonKeyExists(pri, key) { if(typeof pri === 'undefined' || pri === null) { return false; - }else{ + }else { if(jurl.hasOwnProperty(key)) { return true; } } } +function jsonExists(pri) { + if(typeof pri === 'undefined' || pri === null) { return false; }else { return true; } +} + function decode_resp(data) { if (data[0] == 'W'.charCodeAt(0) && data[1] == 'B'.charCodeAt(0)) { var code = data[2] | (data[3] << 8);