vnc_lite.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <!--
  5. noVNC example: lightweight example using minimal UI and features
  6. This is a self-contained file which doesn't import WebUtil or external CSS.
  7. Copyright (C) 2019 The noVNC Authors
  8. noVNC is licensed under the MPL 2.0 (see LICENSE.txt)
  9. This file is licensed under the 2-Clause BSD license (see LICENSE.txt).
  10. Connect parameters are provided in query string:
  11. http://example.com/?host=HOST&port=PORT&scale=true
  12. -->
  13. <title>noVNC</title>
  14. <meta charset="utf-8">
  15. <style>
  16. body {
  17. margin: 0;
  18. background-color: dimgrey;
  19. height: 100%;
  20. display: flex;
  21. flex-direction: column;
  22. }
  23. html {
  24. height: 100%;
  25. }
  26. #top_bar {
  27. background-color: #6e84a3;
  28. color: white;
  29. font: bold 12px Helvetica;
  30. padding: 6px 5px 4px 5px;
  31. border-bottom: 1px outset;
  32. }
  33. #status {
  34. text-align: center;
  35. }
  36. #sendCtrlAltDelButton {
  37. position: fixed;
  38. top: 0px;
  39. right: 0px;
  40. border: 1px outset;
  41. padding: 5px 5px 4px 5px;
  42. cursor: pointer;
  43. }
  44. #screen {
  45. flex: 1; /* fill remaining space */
  46. overflow: hidden;
  47. }
  48. </style>
  49. <script type="module" crossorigin="anonymous">
  50. // RFB holds the API to connect and communicate with a VNC server
  51. import RFB from './core/rfb.js';
  52. let rfb;
  53. let desktopName;
  54. // When this function is called we have
  55. // successfully connected to a server
  56. function connectedToServer(e) {
  57. status("Connected to " + desktopName);
  58. }
  59. // This function is called when we are disconnected
  60. function disconnectedFromServer(e) {
  61. if (e.detail.clean) {
  62. status("Disconnected");
  63. } else {
  64. status("Something went wrong, connection is closed");
  65. }
  66. }
  67. // When this function is called, the server requires
  68. // credentials to authenticate
  69. function credentialsAreRequired(e) {
  70. const password = prompt("Password Required:");
  71. rfb.sendCredentials({ password: password });
  72. }
  73. // When this function is called we have received
  74. // a desktop name from the server
  75. function updateDesktopName(e) {
  76. desktopName = e.detail.name;
  77. }
  78. // Since most operating systems will catch Ctrl+Alt+Del
  79. // before they get a chance to be intercepted by the browser,
  80. // we provide a way to emulate this key sequence.
  81. function sendCtrlAltDel() {
  82. rfb.sendCtrlAltDel();
  83. return false;
  84. }
  85. // Show a status text in the top bar
  86. function status(text) {
  87. document.getElementById('status').textContent = text;
  88. }
  89. // This function extracts the value of one variable from the
  90. // query string. If the variable isn't defined in the URL
  91. // it returns the default value instead.
  92. function readQueryVariable(name, defaultValue) {
  93. // A URL with a query parameter can look like this (But will most probably get logged on the http server):
  94. // https://www.example.com?myqueryparam=myvalue
  95. //
  96. // For privacy (Using a hastag #, the parameters will not be sent to the server)
  97. // the url can be requested in the following way:
  98. // https://www.example.com#myqueryparam=myvalue&password=secreatvalue
  99. //
  100. // Even Mixing public and non public parameters will work:
  101. // https://www.example.com?nonsecretparam=example.com#password=secreatvalue
  102. //
  103. // Note that we use location.href instead of location.search
  104. // because Firefox < 53 has a bug w.r.t location.search
  105. const re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
  106. match = ''.concat(document.location.href, window.location.hash).match(re);
  107. if (match) {
  108. // We have to decode the URL since want the cleartext value
  109. return decodeURIComponent(match[1]);
  110. }
  111. return defaultValue;
  112. }
  113. document.getElementById('sendCtrlAltDelButton')
  114. .onclick = sendCtrlAltDel;
  115. // Read parameters specified in the URL query string
  116. // By default, use the host and port of server that served this file
  117. const host = readQueryVariable('host', window.location.hostname);
  118. let port = readQueryVariable('port', window.location.port);
  119. const password = readQueryVariable('password');
  120. const path = readQueryVariable('path', 'websockify');
  121. // | | | | | |
  122. // | | | Connect | | |
  123. // v v v v v v
  124. status("Connecting");
  125. // Build the websocket URL used to connect
  126. let url;
  127. if (window.location.protocol === "https:") {
  128. url = 'wss';
  129. } else {
  130. url = 'ws';
  131. }
  132. url += '://' + host;
  133. if(port) {
  134. url += ':' + port;
  135. }
  136. url += '/' + path;
  137. // Creating a new RFB object will start a new connection
  138. rfb = new RFB(document.getElementById('screen'), url,
  139. { credentials: { password: password } });
  140. // Add listeners to important events from the RFB module
  141. rfb.addEventListener("connect", connectedToServer);
  142. rfb.addEventListener("disconnect", disconnectedFromServer);
  143. rfb.addEventListener("credentialsrequired", credentialsAreRequired);
  144. rfb.addEventListener("desktopname", updateDesktopName);
  145. // Set parameters that can be changed on an active connection
  146. rfb.viewOnly = readQueryVariable('view_only', false);
  147. rfb.scaleViewport = readQueryVariable('scale', false);
  148. </script>
  149. </head>
  150. <body>
  151. <div id="top_bar">
  152. <div id="status">Loading</div>
  153. <div id="sendCtrlAltDelButton">Send CtrlAltDel</div>
  154. </div>
  155. <div id="screen">
  156. <!-- This is where the remote screen will appear -->
  157. </div>
  158. </body>
  159. </html>