novnc.tpl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. {literal}
  50. <script type="module" crossorigin="anonymous">
  51. // RFB holds the API to connect and communicate with a VNC server
  52. import RFB from '{/literal}{$novncAppUrl}{literal}/core/rfb.js';
  53. let rfb;
  54. let desktopName;
  55. // When this function is called we have
  56. // successfully connected to a server
  57. function connectedToServer(e) {
  58. status("Connected to " + desktopName);
  59. }
  60. // This function is called when we are disconnected
  61. function disconnectedFromServer(e) {
  62. if (e.detail.clean) {
  63. status("Disconnected");
  64. } else {
  65. status("Something went wrong, connection is closed");
  66. }
  67. }
  68. // When this function is called, the server requires
  69. // credentials to authenticate
  70. function credentialsAreRequired(e) {
  71. const password = prompt("Password Required:");
  72. rfb.sendCredentials({ password: password });
  73. }
  74. // When this function is called we have received
  75. // a desktop name from the server
  76. function updateDesktopName(e) {
  77. desktopName = e.detail.name;
  78. }
  79. // Since most operating systems will catch Ctrl+Alt+Del
  80. // before they get a chance to be intercepted by the browser,
  81. // we provide a way to emulate this key sequence.
  82. function sendCtrlAltDel() {
  83. rfb.sendCtrlAltDel();
  84. return false;
  85. }
  86. // Show a status text in the top bar
  87. function status(text) {
  88. document.getElementById('status').textContent = text;
  89. }
  90. // This function extracts the value of one variable from the
  91. // query string. If the variable isn't defined in the URL
  92. // it returns the default value instead.
  93. function readQueryVariable(name, defaultValue) {
  94. // A URL with a query parameter can look like this:
  95. // https://www.example.com?myqueryparam=myvalue
  96. //
  97. // Note that we use location.href instead of location.search
  98. // because Firefox < 53 has a bug w.r.t location.search
  99. const re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
  100. match = document.location.href.match(re);
  101. if (match) {
  102. // We have to decode the URL since want the cleartext value
  103. return decodeURIComponent(match[1]);
  104. }
  105. return defaultValue;
  106. }
  107. document.getElementById('sendCtrlAltDelButton')
  108. .onclick = sendCtrlAltDel;
  109. // | | | | | |
  110. // | | | Connect | | |
  111. // v v v v v v
  112. status("Connecting");
  113. try{
  114. var error = "{/literal}{$error}{literal}";
  115. if (error && error !== undefined && error !== null) {
  116. throw new Error(error);
  117. }
  118. //connect
  119. // Creating a new RFB object will start a new connection
  120. rfb = new RFB(document.getElementById('screen'), "{/literal}{$websocketUrl}{literal}",
  121. { credentials: { password: "{/literal}{$password}{literal}" } });
  122. // Add listeners to important events from the RFB module
  123. rfb.addEventListener("connect", connectedToServer);
  124. rfb.addEventListener("disconnect", disconnectedFromServer);
  125. rfb.addEventListener("credentialsrequired", credentialsAreRequired);
  126. rfb.addEventListener("desktopname", updateDesktopName);
  127. // Set parameters that can be changed on an active connection
  128. rfb.viewOnly = readQueryVariable('view_only', false);
  129. rfb.scaleViewport = readQueryVariable('scale', false);
  130. }catch (e) {
  131. status(e);
  132. }
  133. </script>
  134. {/literal}
  135. </head>
  136. <body>
  137. <div id="top_bar">
  138. <div id="status">Loading</div>
  139. <div id="sendCtrlAltDelButton">Send CtrlAltDel</div>
  140. </div>
  141. <div id="screen">
  142. <!-- This is where the remote screen will appear -->
  143. </div>
  144. </body>
  145. </html>