import { AdbClient , KeyStore , Options , Shell , WebUsbTransport } from 'wadb' ; const connectButton = document . querySelector ( '#connect' ) ! ; const disconnectButton = document . querySelector ( '#disconnect' ) ! ; const output = document . querySelector ( '#output' ) ! ; const input = ( document . querySelector ( '#input' ) as HTMLInputElement ) ! ; class MyKeyStore implements KeyStore { private keys : CryptoKeyPair [ ] = [ ] ; async loadKeys ( ) { return this . keys ; } async saveKey ( key ) { this . keys . push ( key ) ; console . log ( 'Saving Key' + key ) ; } } const options = { debug : true , useChecksum : false , dump : false , keySize : 2048 , } ; const keyStore = new MyKeyStore ( ) ; let transport = null ; let adbClient = null ; let shell = null ; function appendToCode ( text ) { const span = document . createElement ( 'span' ) ; span . innerText = text ; output . appendChild ( span ) ; output . scrollTop = output . scrollHeight ; } function sendCommand ( cmd ) { shell ! . write ( cmd + '\n' ) ; } connectButton . addEventListener ( 'click' , async ( e ) => { try { transport = await WebUsbTransport . open ( options ) ; adbClient = new AdbClient ( transport , options , keyStore ) ; await adbClient . connect ( ) ; shell = await adbClient . interactiveShell ( appendToCode ) ; disconnectButton . classList . toggle ( 'hidden' ) ; connectButton . classList . toggle ( 'hidden' ) ; } catch ( e ) { console . error ( 'Connection Failed: ' , e ) ; } } ) ; disconnectButton . addEventListener ( 'click' , async ( e ) => { try { await shell ?. close ( ) ; await transport ?. close ( ) ; transport = null ; adbClient = null ; shell = null ; } catch ( e ) { console . error ( 'Error closing the connection' , e ) ; } disconnectButton . classList . toggle ( 'hidden' ) ; connectButton . classList . toggle ( 'hidden' ) ; } ) ; input . addEventListener ( 'keyup' , ( e ) => { if ( e . keyCode === 13 ) { e . preventDefault ( ) ; sendCommand ( input . value ) ; input . value = '' ; return false ; } return true ; } ) ;