6 noVNC example: lightweight example using minimal UI and features
8 This is a self-contained file which doesn't import WebUtil or external CSS.
10 Copyright (C) 2018 The noVNC Authors
11 noVNC is licensed under the MPL 2.0 (see LICENSE.txt)
12 This file is licensed under the 2-Clause BSD license (see LICENSE.txt).
14 Connect parameters are provided in query string:
15 http://example.com/?host=HOST&port=PORT&scale=true
19 <meta charset=
"utf-8">
21 <!-- Always force latest IE rendering engine (even in intranet) &
22 Chrome Frame. Remove this if you use the .htaccess -->
23 <meta http-equiv=
"X-UA-Compatible" content=
"IE=edge,chrome=1">
25 <style type=
"text/css">
29 background-color: dimgrey;
32 flex-direction: column;
39 background-color: #
6e84a3;
41 font: bold
12px Helvetica;
42 padding:
6px
5px
4px
5px;
43 border-bottom:
1px outset;
48 #sendCtrlAltDelButton {
53 padding:
5px
5px
4px
5px;
58 flex:
1; /* fill remaining space */
64 <!-- Promise polyfill for IE11 -->
65 <script src=
"vendor/promise.js"></script>
67 <!-- ES2015/ES6 modules polyfill -->
68 <script type=
"module">
69 window._noVNC_has_module_support = true;
72 window.addEventListener(
"load", function() {
73 if (window._noVNC_has_module_support) return;
74 const loader = document.createElement(
"script");
75 loader.src =
"vendor/browser-es-module-loader/dist/" +
76 "browser-es-module-loader.js";
77 document.head.appendChild(loader);
81 <!-- actual script modules -->
82 <script type=
"module" crossorigin=
"anonymous">
83 // RFB holds the API to connect and communicate with a VNC server
84 import RFB from './core/rfb.js';
89 // When this function is called we have
90 // successfully connected to a server
91 function connectedToServer(e) {
92 status(
"Connected to " + desktopName);
95 // This function is called when we are disconnected
96 function disconnectedFromServer(e) {
98 status(
"Disconnected");
100 status(
"Something went wrong, connection is closed");
104 // When this function is called, the server requires
105 // credentials to authenticate
106 function credentialsAreRequired(e) {
107 const password = prompt(
"Password Required:");
108 rfb.sendCredentials({ password: password });
111 // When this function is called we have received
112 // a desktop name from the server
113 function updateDesktopName(e) {
114 desktopName = e.detail.name;
117 // Since most operating systems will catch Ctrl+Alt+Del
118 // before they get a chance to be intercepted by the browser,
119 // we provide a way to emulate this key sequence.
120 function sendCtrlAltDel() {
121 rfb.sendCtrlAltDel();
125 // Show a status text in the top bar
126 function status(text) {
127 document.getElementById('status').textContent = text;
130 // This function extracts the value of one variable from the
131 // query string. If the variable isn't defined in the URL
132 // it returns the default value instead.
133 function readQueryVariable(name, defaultValue) {
134 // A URL with a query parameter can look like this:
135 // https://www.example.com?myqueryparam=myvalue
137 // Note that we use location.href instead of location.search
138 // because Firefox <
53 has a bug w.r.t location.search
139 const re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
140 match = document.location.href.match(re);
141 if (typeof defaultValue === 'undefined') { defaultValue = null; }
144 // We have to decode the URL since want the cleartext value
145 return decodeURIComponent(match[
1]);
151 document.getElementById('sendCtrlAltDelButton')
152 .onclick = sendCtrlAltDel;
154 // Read parameters specified in the URL query string
155 // By default, use the host and port of server that served this file
156 const host = readQueryVariable('host', window.location.hostname);
157 let port = readQueryVariable('port', window.location.port);
158 const password = readQueryVariable('password', '');
159 const path = readQueryVariable('path', 'websockify');
162 // | | | Connect | | |
165 status(
"Connecting");
167 // Build the websocket URL used to connect
169 if (window.location.protocol ===
"https:") {
180 // Creating a new RFB object will start a new connection
181 rfb = new RFB(document.getElementById('screen'), url,
182 { credentials: { password: password } });
184 // Add listeners to important events from the RFB module
185 rfb.addEventListener(
"connect", connectedToServer);
186 rfb.addEventListener(
"disconnect", disconnectedFromServer);
187 rfb.addEventListener(
"credentialsrequired", credentialsAreRequired);
188 rfb.addEventListener(
"desktopname", updateDesktopName);
190 // Set parameters that can be changed on an active connection
191 rfb.viewOnly = readQueryVariable('view_only', false);
192 rfb.scaleViewport = readQueryVariable('scale', false);
198 <div id=
"status">Loading
</div>
199 <div id=
"sendCtrlAltDelButton">Send CtrlAltDel
</div>
202 <!-- This is where the remote screen will appear -->