terminal.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <html>
  2. <head>
  3. <title>NodeMUD</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. <style type="text/css">
  7. html,body {
  8. background-color: black;
  9. color: white;
  10. margin: 0;
  11. padding: 0;
  12. }
  13. #output {
  14. font-family: 'Inconsolata', monospace;
  15. white-space: pre-wrap;
  16. padding: 8px;
  17. }
  18. #input {
  19. font-family: 'Inconsolata', monospace;
  20. background-color: black;
  21. color: white;
  22. border: 0px;
  23. padding: 8px;
  24. width: 100%;
  25. }
  26. .fgblack {
  27. color: black;
  28. }
  29. .fgred {
  30. color: red;
  31. }
  32. .fggreen {
  33. color: green;
  34. }
  35. .fgyellow {
  36. color: yellow;
  37. }
  38. .fgblue {
  39. color: blue;
  40. }
  41. .fgmagenta {
  42. color: magenta;
  43. }
  44. .fgcyan {
  45. color: cyan;
  46. }
  47. .fgwhite {
  48. color: white;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <div id="output"></div>
  54. <input type="text" id="input" />
  55. <script>
  56. var localEcho = true;
  57. var commandHistory = [];
  58. var commandPointer = 0;
  59. function handleInstructions(instruction) {
  60. switch(instruction) {
  61. case "protectinput":
  62. document.getElementById("input").attributes["type"].value = "password";
  63. localEcho = false;
  64. break;
  65. case "openinput":
  66. document.getElementById("input").attributes["type"].value = "text";
  67. localEcho = true;
  68. break;
  69. }
  70. }
  71. window.WebSocket = window.WebSocket || window.MozWebSocket;
  72. //var socketString = "ws://" + window.location.hostname + ":" + window.location.port;
  73. var socketString = "ws://" + window.location.hostname + ":8080";
  74. var connection = new WebSocket(socketString);
  75. connection.onopen = function () {
  76. };
  77. connection.onerror = function (error) {
  78. console.error(error);
  79. };
  80. connection.onmessage = function (message) {
  81. try {
  82. var json = JSON.parse(message.data);
  83. if(json.type == "output") {
  84. var text = document.createElement("div");
  85. text.innerHTML = json.data.trim();
  86. document.getElementById("output").appendChild(text);
  87. window.scrollTo(0,document.body.scrollHeight);
  88. } else if(json.type == "instruction") {
  89. handleInstructions(json.data);
  90. } else {
  91. console.log(json);
  92. }
  93. } catch (e) {
  94. console.error("This doesn't look like a valid JSON ", message.data);
  95. return;
  96. }
  97. // handle incoming message
  98. };
  99. connection.onclose = function () {
  100. document.getElementById("output").innerHTML += "connection closed by host\n";
  101. window.scrollTo(0,document.body.scrollHeight);
  102. document.getElementById("input").disabled = true;
  103. };
  104. document.addEventListener("keydown", function(event) {
  105. document.getElementById("input").focus();
  106. if(document.getElementById("input").disabled) {
  107. return true;
  108. }
  109. if(event.keyCode == 13) {
  110. var inputToSend = document.getElementById("input").value;
  111. if(localEcho) {
  112. var text = document.createElement("div");
  113. text.innerHTML = inputToSend.trim();
  114. document.getElementById("output").appendChild(text);
  115. commandHistory.push(inputToSend);
  116. commandPointer = commandHistory.length;
  117. } else {
  118. var text = document.createElement("div");
  119. text.innerHTML = new Array(inputToSend.length + 1).join("*");
  120. document.getElementById("output").appendChild(text);
  121. }
  122. connection.send(inputToSend);
  123. document.getElementById("input").value = "";
  124. } else if (event.keyCode == 38) {
  125. commandPointer--;
  126. commandPointer = Math.max(0, commandPointer);
  127. document.getElementById("input").value = commandHistory[commandPointer];
  128. } else if (event.keyCode == 40) {
  129. commandPointer++;
  130. if(commandPointer >= commandHistory.length) {
  131. commandPointer = Math.min(commandPointer, commandHistory.length - 1);
  132. document.getElementById("input").value = "";
  133. return;
  134. }
  135. commandPointer = Math.min(commandPointer, commandHistory.length - 1);
  136. document.getElementById("input").value = commandHistory[commandPointer];
  137. }
  138. }, false);
  139. document.getElementById("input").focus();
  140. </script>
  141. </body>
  142. </html>