import { WebUsbTransport , AdbClient , Options , KeyStore , Stream } from 'wadb' ; let transport ; let adbClient ; const connectButton = document . querySelector ( '#connect' ) ! ; const disconnectButton = document . querySelector ( '#disconnect' ) ! ; const startButton = document . querySelector ( '#start' ) ! ; const stopButton = document . querySelector ( '#stop' ) ! ; const video = ( document . querySelector ( '#video' ) as HTMLVideoElement ) ! ; const download = ( document . querySelector ( '#download' ) as HTMLAnchorElement ) ! ; const status = document . querySelector ( '#status' ) ! ; const options = { debug : true , useChecksum : false , dump : false , keySize : 2048 , } ; 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 keyStore = new MyKeyStore ( ) ; connectButton . addEventListener ( 'click' , async ( _ ) => { try { transport = await WebUsbTransport . open ( options ) ; adbClient = new AdbClient ( transport , options , keyStore ) ; status . textContent = 'Accept prompt on device' ; const adbConnectionInformation = await adbClient . connect ( ) ; status . textContent = 'Connected and ready' ; console . log ( 'Connected: ' , adbConnectionInformation ) ; connectButton . classList . toggle ( 'hidden' ) ; disconnectButton . classList . toggle ( 'hidden' ) ; } catch ( e ) { console . error ( 'Connection Failed: ' , e ) ; status . textContent = 'Failed to connect to a device' ; } } ) ; disconnectButton . addEventListener ( 'click' , async ( _ ) => { try { if ( adbClient ) { try { await adbClient . disconnect ( ) ; } catch ( e ) { console . log ( 'Error disconnecting ADB Client: ' , e ) ; } adbClient = null ; } if ( transport ) { await transport . close ( ) ; transport = null ; } connectButton . classList . toggle ( 'hidden' ) ; disconnectButton . classList . toggle ( 'hidden' ) ; status . textContent = 'Connect to a device to start' ; } catch ( e ) { console . error ( 'Disconnecting Failed: ' , e ) ; } } ) ; let shell = null ; startButton . addEventListener ( 'click' , async ( ) => { status . textContent = 'Recording...' ; stopButton . classList . toggle ( 'hidden' ) ; startButton . classList . toggle ( 'hidden' ) ; const textDecoder = new TextDecoder ( ) ; shell = await Stream . open ( adbClient ! , 'exec:screenrecord --output-format=h264 -' , options ) ; const chunks = [ ] ; let i = 0 ; let msg ; while ( true ) { console . log ( ++ i ) ; msg = await shell ! . read ( ) ; await shell ! . write ( 'OKAY' ) ; if ( msg . header . cmd === 'CLSE' ) { break ; } console . log ( textDecoder . decode ( msg . data ! . buffer ) ) ; chunks . push ( new Uint8Array ( msg . data ! . buffer ) ) ; } console . log ( chunks . length ) ; const objectUrl = URL . createObjectURL ( new Blob ( chunks ) ) ; video . src = objectUrl ; download . href = objectUrl ; } ) ; stopButton . addEventListener ( 'click' , async ( ) => { await shell ?. write ( 'CLSE' ) ; } ) ;