terminal.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <html>
  2. <head>
  3. <title>Starmire</title>
  4. <link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <link rel="stylesheet" media="screen" href="./css/terminal.css">
  7. </head>
  8. <body>
  9. <div id="output"></div>
  10. <input type="text" id="input" autocomplete="off" />
  11. <script>
  12. var localEcho = true;
  13. var commandHistory = [];
  14. var commandPointer = 0;
  15. function handleInstructions(instruction) {
  16. switch(instruction) {
  17. case "protectinput":
  18. document.getElementById("input").attributes["type"].value = "password";
  19. localEcho = false;
  20. break;
  21. case "openinput":
  22. document.getElementById("input").attributes["type"].value = "text";
  23. localEcho = true;
  24. break;
  25. }
  26. }
  27. window.WebSocket = window.WebSocket || window.MozWebSocket;
  28. var scheme = document.location.protocol === "https:" ? "wss" : "ws";
  29. var port = document.location.port ? (":" + document.location.port) : "";
  30. //var socketString = "ws://" + window.location.hostname + ":" + window.location.port;
  31. var socketString = `${scheme}://${window.location.hostname}${port}`;
  32. var connection = new WebSocket(socketString);
  33. connection.onopen = function () {
  34. };
  35. connection.onerror = function (error) {
  36. console.error(error);
  37. };
  38. connection.onmessage = function (message) {
  39. try {
  40. var json = JSON.parse(message.data);
  41. if(json.type == "output") {
  42. var text = document.createElement("div");
  43. text.innerHTML = json.data.trim();
  44. document.getElementById("output").appendChild(text);
  45. window.scrollTo(0,document.body.scrollHeight);
  46. } else if(json.type == "instruction") {
  47. handleInstructions(json.data);
  48. } else {
  49. console.log(json);
  50. }
  51. } catch (e) {
  52. console.error("This doesn't look like a valid JSON ", message.data);
  53. return;
  54. }
  55. // handle incoming message
  56. };
  57. connection.onclose = function () {
  58. document.getElementById("output").innerHTML += "connection closed by host\n";
  59. window.scrollTo(0,document.body.scrollHeight);
  60. document.getElementById("input").disabled = true;
  61. };
  62. document.addEventListener("keydown", function(event) {
  63. document.getElementById("input").focus();
  64. if(document.getElementById("input").disabled) {
  65. return true;
  66. }
  67. if(event.keyCode == 13) {
  68. var inputToSend = document.getElementById("input").value;
  69. if(localEcho) {
  70. var text = document.createElement("div");
  71. text.innerHTML = inputToSend.trim();
  72. document.getElementById("output").appendChild(text);
  73. commandHistory.push(inputToSend);
  74. commandPointer = commandHistory.length;
  75. } else {
  76. var text = document.createElement("div");
  77. text.innerHTML = new Array(inputToSend.length + 1).join("*");
  78. document.getElementById("output").appendChild(text);
  79. }
  80. connection.send(inputToSend);
  81. document.getElementById("input").value = "";
  82. } else if (event.keyCode == 38) {
  83. commandPointer--;
  84. commandPointer = Math.max(0, commandPointer);
  85. document.getElementById("input").value = commandHistory[commandPointer];
  86. document.getElementById("input").setSelectionRange(commandHistory[commandPointer].length, commandHistory[commandPointer].length);
  87. } else if (event.keyCode == 40) {
  88. commandPointer++;
  89. if(commandPointer >= commandHistory.length) {
  90. commandPointer = Math.min(commandPointer, commandHistory.length - 1);
  91. document.getElementById("input").value = "";
  92. return;
  93. }
  94. commandPointer = Math.min(commandPointer, commandHistory.length - 1);
  95. document.getElementById("input").value = commandHistory[commandPointer];
  96. document.getElementById("input").setSelectionRange(commandHistory[commandPointer].length, commandHistory[commandPointer].length);
  97. }
  98. }, false);
  99. document.getElementById("input").focus();
  100. </script>
  101. </body>
  102. </html>