index.html 3.5 KB

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