Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Connect / Auto Login + logo & version display #1

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 87 additions & 6 deletions webrepl.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
margin-bottom: 20px;
font: 20px/1.5 sans-serif;
}

.logo {
position: absolute;
bottom: 10px;
right: 10px;
}
/*
.terminal {
float: left;
Expand Down Expand Up @@ -48,6 +52,7 @@
<form>
<input type="text" name="webrepl_url" id="url" value="ws://192.168.4.1:8266/" />
<input type="submit" id="button" value="Connect" onclick="button_click(); return false;" />
<span id="version"></span>
</form>
<div id="term">
</div>
Expand All @@ -70,11 +75,21 @@

<div class="file-box" id="file-status"><span style="color:#707070">(file operation status)</span></div>

<div class="file-box" id="link-gen" style="margin-top: 20px;"><button onClick='buildLink()'>Generate auto connect / login link</button></div>

</div>

<br clear="both" />
<i>Terminal widget should be focused (text cursor visible) to accept input. Click on it if not.</i><br/>
<i>To paste, press Ctrl+A, then Ctrl+V</i>


<a class="logo" href="http://micropython.org/">
<svg version="1.1" id="MicroPython" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" viewBox="0 0 69 69" width="90px" xml:space="preserve">
<path fill="#ffffff" d="M36,0v54h-3V0H0v69h15V15h3v54h33V15h3v54h15V0H36z M64,63h-4v-7h4V63z"/>
</svg>
</a>
</body>

<script>
Expand All @@ -88,19 +103,54 @@
var put_file_data = null;
var get_file_name = null;
var get_file_data = null;
var version = null;

function calculate_size(win) {
var cols = Math.max(80, Math.min(150, (win.innerWidth - 280) / 7)) | 0;
var rows = Math.max(24, Math.min(80, (win.innerHeight - 180) / 12)) | 0;
return [cols, rows];
}

var jurl = null;

function buildLink()
{
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);


}

(function() {
window.onload = function() {
var url = window.location.hash.substring(1);
if (url) {
try {
jurl = JSON.parse(decodeURI(window.location.hash.substring(1)));
console.log(jurl);

if(jurl.url) {
document.getElementById('url').value = jurl.url;
if(jurl.autoconnect) {
setTimeout(function() { button_click(); }, 100);
}
}

}
catch(error) {
console.log(error);
console.log("Attempting Alt Method of URL parsing.");
var url = window.location.hash.substring(1);
if (url) {
document.getElementById('url').value = 'ws://' + url;
}
}




var size = calculate_size(self);
term = new Terminal({
cols: size[0],
Expand Down Expand Up @@ -152,8 +202,16 @@
document.getElementById('file-status').innerHTML = s;
}

function connect(url) {
window.location.hash = url.substring(5);
function connect(url) {
if(typeof jurl === 'undefined' || jurl === null) {
window.location.hash = url.substring(5);
}else{
if(jurl.hasOwnProperty('url') && jurl.url !== url) {
window.location.hash = url.substring(5);
}
}


ws = new WebSocket(url);
ws.binaryType = 'arraybuffer';
ws.onopen = function() {
Expand All @@ -174,6 +232,9 @@
term.write('\x1b[31mWelcome to MicroPython!\x1b[m\r\n');

ws.onmessage = function(event) {
if (version == undefined && typeof event.data == 'string' && event.data.indexOf('WebREPL connected') > 0) {
get_ver();
}
if (event.data instanceof ArrayBuffer) {
var data = new Uint8Array(event.data);
switch (binary_state) {
Expand Down Expand Up @@ -243,11 +304,14 @@
break;
case 31:
// first (and last) response for GET_VER
console.log('GET_VER', data);
//console.log('GET_VER', data);
version = data.join('.');
document.getElementById('version').innerText = 'MicroPython ' + version;
binary_state = 0;
break;
}
}
managePasswordReq(event.data);
term.write(event.data);
};
};
Expand All @@ -262,6 +326,23 @@
}
}

function managePasswordReq(data) {
if(typeof data == 'string' && data.indexOf('Password:') > -1 && jsonExists(jurl, 'pass')) {
term.write('\x1b[36mAttempting AutoLogin\x1b[m\r\n');
term.send(jurl.pass + "\r");
}
}

function jsonExists(pri, key) {
if(typeof pri === 'undefined' || pri === null) {
return false;
}else{
if(jurl.hasOwnProperty(key)) {
return true;
}
}
}

function decode_resp(data) {
if (data[0] == 'W'.charCodeAt(0) && data[1] == 'B'.charCodeAt(0)) {
var code = data[2] | (data[3] << 8);
Expand Down