var CliHistory = function () { this.history = []; this.index = 0; }; CliHistory.prototype = { add: function (str) { this.history.push(str); this.index = this.history.length; }, prev: function () { if (this.index > 0) this.index -= 1; return this.history[this.index]; }, next: function () { if (this.index < this.history.length) this.index += 1; return this.history[this.index - 1]; } } cli_history = new CliHistory(); function tab_initialize_cli() { ga_tracker.sendAppView('CLI Page'); CLI_active = true; // Enter CLI mode var bufferOut = new ArrayBuffer(1); var bufView = new Uint8Array(bufferOut); bufView[0] = 0x23; // # chrome.serial.write(connectionId, bufferOut, function(writeInfo) { }); var textarea = $('.tab-cli textarea'); textarea.keypress(function(event) { if (event.which == 13) { // enter var out_string = $('.tab-cli textarea').val(); var out_arr = out_string.split("\n"); cli_history.add(out_string.trim()); var timeout_needle = 0; for (var i = 0; i < out_arr.length; i++) { send_slowly(out_arr, i, timeout_needle++); } $('.tab-cli textarea').val(''); } }); textarea.keyup(function(event) { var keyUp = { 38: true }, keyDown = { 40: true }; if (event.keyCode in keyUp) textarea.val(cli_history.prev()); if (event.keyCode in keyDown) textarea.val(cli_history.next()); }); // give input element user focus $('.tab-cli textarea').focus(); // if user clicks inside the console window, input element gets re-focused $('.tab-cli .window').click(function() { $('.tab-cli textarea').focus(); }); $('.tab-cli .copy').click(function() { var text = $('.tab-cli .window .wrapper').html(); text = text.replace(//mg,"\n"); // replacing br tags with \n to keep some of the formating var copyFrom = $('