Terminal

<html>
<head>
<title>Terminal</title>
http://extension.js
http://ws.js
</head>

<script Language=”Javascript”>

//Global vars.
var debug = false
var maxLines = 100
var editLine = “”
var lastKey = “”
var waiting = false
var ws = null;
var log = new Array()
log.push(“”)

//Initialise the extension.
ext.Init()

//Called when extension is ready.
function ext_OnReady()
{
    //Create websocket to receive terminal data.
    var ip = ext.GetServerIp();
    ws = CreateWebSocket( “ws”, ip, debug?8087:8889, “NoReconnect,KeepAlive” )
    ws.SetOnMessage( Log )
    ws.SetOnOpen( ws_OnOpen )
}

//Called when socket is open.
function ws_OnOpen( id )
{
   Log( “n” )
}

//Log data to screen.
function Log( msg )
{
    console.log( msg )
    var cmd = msg.substr(0,2)
    msg = msg.substr(2)
   
    if( cmd==”I:” )
    {
        var lines = msg.split(“n”)
        for( var i=0; i< lines.length; i++ )
        {
            if( log.length >= maxLines ) log.shift();
            log.push( lines[i] + (i<lines.length-1?”n”:””) )
        }
    }
else if( cmd==”E:” )
{
    var err = msg.split(“:”)
    log.push( err.slice(2) )
}
txtLog.value = log.join(“”) + “>”
txtLog.scrollTop = txtLog.scrollHeight;
waiting = false
}

//Write text to console.
function Write( txt,send )
{
editLine += txt
txtLog.value += txt
txtLog.scrollTop = txtLog.scrollHeight;
if( send ) txtLog_OnKeyPress({keyCode:13})
}

//Handle key presses
function txtLog_OnKeyPress( e )
{
    //if( waiting ) return
   
var key = String.fromCharCode(e.keyCode)
//console.log( key  )

if( e.keyCode==13 ) {
    if( !editLine ) { Log(“I:>n”); return }
    ws.Send(editLine+”n”)
    Log(“I:>”+editLine+”n”)
    editLine=””
    waiting=true
    //setTimeout( function(){if(waiting)waiting=false;},3000 )
}
else {
    //log[log.length-1] += key
    //txtLog.value = log.join(“”)
    Write( key )
}

//else ws.Send( key );
lastKey = e.keyCode
}

//Handle special keys.
function txtLog_OnKeyDown( e )
{
    //console.log( e.keyCode )
    if( e.keyCode==8 && editLine.length > 0 )
    {
        //ws.Send(“b”)
        //ws.Send(“x04”)
        editLine = editLine.slice(0,-1)
        txtLog.value = txtLog.value.slice(0,-1)
    }
}

//Handle pasting.
function txtLog_OnPaste( e )
{
    e.preventDefault();
    var text = (e.originalEvent || e).clipboardData.getData(‘text/plain’);
    //alert( text )
    //document.execCommand(“insertHTML”, false, text);
    Write( text )
}

//Handle buttons.
function btn_Command( val )
{
    console.log( val )
    if( val==”Reset” ) { try{ ws.Send(“@RESET@”)}catch(e){}; location.reload() }
    else if( val==”Ctrl+D” ) { if( ws ) { ws.Send( “@RESET@” ); ws.Send(“n”) } }
    else if( val==”Ctrl+C” ) { if( ws ) ws.Send( “@EOS@” ) }
    else Write(val,true)
}
</script>

<body style=”font-family: Arial; font-size: 9pt” bgcolor=#f4f4f4 >

    <input type=”button” value=”Reset” onclick=”btn_Command(this.value)”>
    &nbsp;&nbsp;&nbsp;
    <input type=”button” value=”cd /” onclick=”btn_Command(this.value)”>
    <input type=”button” value=”cd /sdcard” onclick=”btn_Command(this.value)”>
    &nbsp;&nbsp;&nbsp;
    <input type=”button” value=”pwd” onclick=”btn_Command(this.value)”>
    <input type=”button” value=”ls” onclick=”btn_Command(this.value)”>
<input type=”button” value=”netstat” onclick=”btn_Command(this.value)”>
   
    <!–<input type=”button” value=”Ctrl+D” onclick=”btn_Command(this.value)”>
    <input type=”button” value=”Ctrl+C” onclick=”btn_Command(this.value)”>–>
   
<textarea id=”txtLog” style=”width:100%; height:95%; resize: none;”
    readonly onKeyPress=”txtLog_OnKeyPress(event)” onKeyDown=”txtLog_OnKeyDown(event)” onPaste=”txtLog_OnPaste(event)”></textarea>

</body>
</html>


Posted

in

,

by

%d bloggers like this: