2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2018 The noVNC Authors
4 * Licensed under MPL 2.0 (see LICENSE.txt)
6 * See README.md for usage and integration instructions.
9 import * as Log
from '../core/util/logging.js';
10 import _
, { l10n
} from './localization.js';
11 import { isTouchDevice
, dragThreshold
} from '../core/util/browser.js';
12 import { setCapture
, getPointerEvent
} from '../core/util/events.js';
13 import KeyTable
from "../core/input/keysym.js";
14 import keysyms
from "../core/input/keysymdef.js";
15 import Keyboard
from "../core/input/keyboard.js";
16 import RFB
from "../core/rfb.js";
17 import * as WebUtil
from "./webutil.js";
25 hideKeyboardTimeout
: null,
26 idleControlbarTimeout
: null,
27 closeControlbarTimeout
: null,
29 controlbarGrabbed
: false,
30 controlbarDrag
: false,
31 controlbarMouseDownClientY
: 0,
32 controlbarMouseDownOffsetY
: 0,
35 lastKeyboardinput
: null,
36 defaultKeyboardinputLen
: 100,
38 inhibit_reconnect
: true,
39 reconnect_callback
: null,
40 reconnect_password
: null,
43 if (document
.readyState
=== "interactive" || document
.readyState
=== "complete") {
46 document
.addEventListener('DOMContentLoaded', UI
.load
.bind(UI
, callback
));
50 // Setup rfb object, load settings from browser storage, then call
51 // UI.init to setup the UI/menus
53 WebUtil
.initSettings(UI
.start
, callback
);
56 // Render default UI and initialize settings menu
59 // Setup global variables first
60 UI
.isSafari
= (navigator
.userAgent
.indexOf('Safari') !== -1 &&
61 navigator
.userAgent
.indexOf('Chrome') === -1);
68 // Adapt the interface for touch screen devices
70 document
.documentElement
.classList
.add("noVNC_touch");
71 // Remove the address bar
72 setTimeout(() => window
.scrollTo(0, 1), 100);
75 // Restore control bar position
76 if (WebUtil
.readSetting('controlbar_pos') === 'right') {
77 UI
.toggleControlbarSide();
82 // Setup event handlers
83 UI
.addControlbarHandlers();
84 UI
.addTouchSpecificHandlers();
85 UI
.addExtraKeysHandlers();
86 UI
.addMachineHandlers();
87 UI
.addConnectionControlHandlers();
88 UI
.addClipboardHandlers();
89 UI
.addSettingsHandlers();
90 document
.getElementById("noVNC_status")
91 .addEventListener('click', UI
.hideStatus
);
93 // Bootstrap fallback input handler
94 UI
.keyboardinputReset();
98 UI
.updateVisualState('init');
100 document
.documentElement
.classList
.remove("noVNC_loading");
102 let autoconnect
= WebUtil
.getConfigVar('autoconnect', false);
103 if (autoconnect
=== 'true' || autoconnect
== '1') {
108 // Show the connect panel on first load unless autoconnecting
109 UI
.openConnectPanel();
112 if (typeof callback
=== "function") {
118 // Only show the button if fullscreen is properly supported
119 // * Safari doesn't support alphanumerical input while in fullscreen
121 (document
.documentElement
.requestFullscreen
||
122 document
.documentElement
.mozRequestFullScreen
||
123 document
.documentElement
.webkitRequestFullscreen
||
124 document
.body
.msRequestFullscreen
)) {
125 document
.getElementById('noVNC_fullscreen_button')
126 .classList
.remove("noVNC_hidden");
127 UI
.addFullscreenHandlers();
132 // Logging selection dropdown
133 const llevels
= ['error', 'warn', 'info', 'debug'];
134 for (let i
= 0; i
< llevels
.length
; i
+= 1) {
135 UI
.addOption(document
.getElementById('noVNC_setting_logging'), llevels
[i
], llevels
[i
]);
138 // Settings with immediate effects
139 UI
.initSetting('logging', 'warn');
142 // if port == 80 (or 443) then it won't be present and should be
144 let port
= window
.location
.port
;
146 if (window
.location
.protocol
.substring(0, 5) == 'https') {
148 } else if (window
.location
.protocol
.substring(0, 4) == 'http') {
153 /* Populate the controls if defaults are provided in the URL */
154 UI
.initSetting('host', window
.location
.hostname
);
155 UI
.initSetting('port', port
);
156 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
157 UI
.initSetting('view_clip', false);
158 UI
.initSetting('resize', 'off');
159 UI
.initSetting('shared', true);
160 UI
.initSetting('view_only', false);
161 UI
.initSetting('show_dot', false);
162 UI
.initSetting('path', 'websockify');
163 UI
.initSetting('repeaterID', '');
164 UI
.initSetting('reconnect', false);
165 UI
.initSetting('reconnect_delay', 5000);
167 UI
.setupSettingLabels();
169 // Adds a link to the label elements on the corresponding input elements
170 setupSettingLabels() {
171 const labels
= document
.getElementsByTagName('LABEL');
172 for (let i
= 0; i
< labels
.length
; i
++) {
173 const htmlFor
= labels
[i
].htmlFor
;
175 const elem
= document
.getElementById(htmlFor
);
176 if (elem
) elem
.label
= labels
[i
];
178 // If 'for' isn't set, use the first input element child
179 const children
= labels
[i
].children
;
180 for (let j
= 0; j
< children
.length
; j
++) {
181 if (children
[j
].form
!== undefined) {
182 children
[j
].label
= labels
[i
];
196 addControlbarHandlers() {
197 document
.getElementById("noVNC_control_bar")
198 .addEventListener('mousemove', UI
.activateControlbar
);
199 document
.getElementById("noVNC_control_bar")
200 .addEventListener('mouseup', UI
.activateControlbar
);
201 document
.getElementById("noVNC_control_bar")
202 .addEventListener('mousedown', UI
.activateControlbar
);
203 document
.getElementById("noVNC_control_bar")
204 .addEventListener('keydown', UI
.activateControlbar
);
206 document
.getElementById("noVNC_control_bar")
207 .addEventListener('mousedown', UI
.keepControlbar
);
208 document
.getElementById("noVNC_control_bar")
209 .addEventListener('keydown', UI
.keepControlbar
);
211 document
.getElementById("noVNC_view_drag_button")
212 .addEventListener('click', UI
.toggleViewDrag
);
214 document
.getElementById("noVNC_control_bar_handle")
215 .addEventListener('mousedown', UI
.controlbarHandleMouseDown
);
216 document
.getElementById("noVNC_control_bar_handle")
217 .addEventListener('mouseup', UI
.controlbarHandleMouseUp
);
218 document
.getElementById("noVNC_control_bar_handle")
219 .addEventListener('mousemove', UI
.dragControlbarHandle
);
220 // resize events aren't available for elements
221 window
.addEventListener('resize', UI
.updateControlbarHandle
);
223 const exps
= document
.getElementsByClassName("noVNC_expander");
224 for (let i
= 0;i
< exps
.length
;i
++) {
225 exps
[i
].addEventListener('click', UI
.toggleExpander
);
229 addTouchSpecificHandlers() {
230 document
.getElementById("noVNC_mouse_button0")
231 .addEventListener('click', () => UI
.setMouseButton(1));
232 document
.getElementById("noVNC_mouse_button1")
233 .addEventListener('click', () => UI
.setMouseButton(2));
234 document
.getElementById("noVNC_mouse_button2")
235 .addEventListener('click', () => UI
.setMouseButton(4));
236 document
.getElementById("noVNC_mouse_button4")
237 .addEventListener('click', () => UI
.setMouseButton(0));
238 document
.getElementById("noVNC_keyboard_button")
239 .addEventListener('click', UI
.toggleVirtualKeyboard
);
241 UI
.touchKeyboard
= new Keyboard(document
.getElementById('noVNC_keyboardinput'));
242 UI
.touchKeyboard
.onkeyevent
= UI
.keyEvent
;
243 UI
.touchKeyboard
.grab();
244 document
.getElementById("noVNC_keyboardinput")
245 .addEventListener('input', UI
.keyInput
);
246 document
.getElementById("noVNC_keyboardinput")
247 .addEventListener('focus', UI
.onfocusVirtualKeyboard
);
248 document
.getElementById("noVNC_keyboardinput")
249 .addEventListener('blur', UI
.onblurVirtualKeyboard
);
250 document
.getElementById("noVNC_keyboardinput")
251 .addEventListener('submit', () => false);
253 document
.documentElement
254 .addEventListener('mousedown', UI
.keepVirtualKeyboard
, true);
256 document
.getElementById("noVNC_control_bar")
257 .addEventListener('touchstart', UI
.activateControlbar
);
258 document
.getElementById("noVNC_control_bar")
259 .addEventListener('touchmove', UI
.activateControlbar
);
260 document
.getElementById("noVNC_control_bar")
261 .addEventListener('touchend', UI
.activateControlbar
);
262 document
.getElementById("noVNC_control_bar")
263 .addEventListener('input', UI
.activateControlbar
);
265 document
.getElementById("noVNC_control_bar")
266 .addEventListener('touchstart', UI
.keepControlbar
);
267 document
.getElementById("noVNC_control_bar")
268 .addEventListener('input', UI
.keepControlbar
);
270 document
.getElementById("noVNC_control_bar_handle")
271 .addEventListener('touchstart', UI
.controlbarHandleMouseDown
);
272 document
.getElementById("noVNC_control_bar_handle")
273 .addEventListener('touchend', UI
.controlbarHandleMouseUp
);
274 document
.getElementById("noVNC_control_bar_handle")
275 .addEventListener('touchmove', UI
.dragControlbarHandle
);
278 addExtraKeysHandlers() {
279 document
.getElementById("noVNC_toggle_extra_keys_button")
280 .addEventListener('click', UI
.toggleExtraKeys
);
281 document
.getElementById("noVNC_toggle_ctrl_button")
282 .addEventListener('click', UI
.toggleCtrl
);
283 document
.getElementById("noVNC_toggle_alt_button")
284 .addEventListener('click', UI
.toggleAlt
);
285 document
.getElementById("noVNC_send_tab_button")
286 .addEventListener('click', UI
.sendTab
);
287 document
.getElementById("noVNC_send_esc_button")
288 .addEventListener('click', UI
.sendEsc
);
289 document
.getElementById("noVNC_send_ctrl_alt_del_button")
290 .addEventListener('click', UI
.sendCtrlAltDel
);
293 addMachineHandlers() {
294 document
.getElementById("noVNC_shutdown_button")
295 .addEventListener('click', () => UI
.rfb
.machineShutdown());
296 document
.getElementById("noVNC_reboot_button")
297 .addEventListener('click', () => UI
.rfb
.machineReboot());
298 document
.getElementById("noVNC_reset_button")
299 .addEventListener('click', () => UI
.rfb
.machineReset());
300 document
.getElementById("noVNC_power_button")
301 .addEventListener('click', UI
.togglePowerPanel
);
304 addConnectionControlHandlers() {
305 document
.getElementById("noVNC_disconnect_button")
306 .addEventListener('click', UI
.disconnect
);
307 document
.getElementById("noVNC_connect_button")
308 .addEventListener('click', UI
.connect
);
309 document
.getElementById("noVNC_cancel_reconnect_button")
310 .addEventListener('click', UI
.cancelReconnect
);
312 document
.getElementById("noVNC_password_button")
313 .addEventListener('click', UI
.setPassword
);
316 addClipboardHandlers() {
317 document
.getElementById("noVNC_clipboard_button")
318 .addEventListener('click', UI
.toggleClipboardPanel
);
319 document
.getElementById("noVNC_clipboard_text")
320 .addEventListener('change', UI
.clipboardSend
);
321 document
.getElementById("noVNC_clipboard_clear_button")
322 .addEventListener('click', UI
.clipboardClear
);
325 // Add a call to save settings when the element changes,
326 // unless the optional parameter changeFunc is used instead.
327 addSettingChangeHandler(name
, changeFunc
) {
328 const settingElem
= document
.getElementById("noVNC_setting_" + name
);
329 if (changeFunc
=== undefined) {
330 changeFunc
= () => UI
.saveSetting(name
);
332 settingElem
.addEventListener('change', changeFunc
);
335 addSettingsHandlers() {
336 document
.getElementById("noVNC_settings_button")
337 .addEventListener('click', UI
.toggleSettingsPanel
);
339 UI
.addSettingChangeHandler('encrypt');
340 UI
.addSettingChangeHandler('resize');
341 UI
.addSettingChangeHandler('resize', UI
.applyResizeMode
);
342 UI
.addSettingChangeHandler('resize', UI
.updateViewClip
);
343 UI
.addSettingChangeHandler('view_clip');
344 UI
.addSettingChangeHandler('view_clip', UI
.updateViewClip
);
345 UI
.addSettingChangeHandler('shared');
346 UI
.addSettingChangeHandler('view_only');
347 UI
.addSettingChangeHandler('view_only', UI
.updateViewOnly
);
348 UI
.addSettingChangeHandler('show_dot');
349 UI
.addSettingChangeHandler('show_dot', UI
.updateShowDotCursor
);
350 UI
.addSettingChangeHandler('host');
351 UI
.addSettingChangeHandler('port');
352 UI
.addSettingChangeHandler('path');
353 UI
.addSettingChangeHandler('repeaterID');
354 UI
.addSettingChangeHandler('logging');
355 UI
.addSettingChangeHandler('logging', UI
.updateLogging
);
356 UI
.addSettingChangeHandler('reconnect');
357 UI
.addSettingChangeHandler('reconnect_delay');
360 addFullscreenHandlers() {
361 document
.getElementById("noVNC_fullscreen_button")
362 .addEventListener('click', UI
.toggleFullscreen
);
364 window
.addEventListener('fullscreenchange', UI
.updateFullscreenButton
);
365 window
.addEventListener('mozfullscreenchange', UI
.updateFullscreenButton
);
366 window
.addEventListener('webkitfullscreenchange', UI
.updateFullscreenButton
);
367 window
.addEventListener('msfullscreenchange', UI
.updateFullscreenButton
);
376 // Disable/enable controls depending on connection state
377 updateVisualState(state
) {
379 document
.documentElement
.classList
.remove("noVNC_connecting");
380 document
.documentElement
.classList
.remove("noVNC_connected");
381 document
.documentElement
.classList
.remove("noVNC_disconnecting");
382 document
.documentElement
.classList
.remove("noVNC_reconnecting");
384 const transition_elem
= document
.getElementById("noVNC_transition_text");
389 transition_elem
.textContent
= _("Connecting...");
390 document
.documentElement
.classList
.add("noVNC_connecting");
393 document
.documentElement
.classList
.add("noVNC_connected");
395 case 'disconnecting':
396 transition_elem
.textContent
= _("Disconnecting...");
397 document
.documentElement
.classList
.add("noVNC_disconnecting");
402 transition_elem
.textContent
= _("Reconnecting...");
403 document
.documentElement
.classList
.add("noVNC_reconnecting");
406 Log
.Error("Invalid visual state: " + state
);
407 UI
.showStatus(_("Internal error"), 'error');
414 UI
.disableSetting('encrypt');
415 UI
.disableSetting('shared');
416 UI
.disableSetting('host');
417 UI
.disableSetting('port');
418 UI
.disableSetting('path');
419 UI
.disableSetting('repeaterID');
420 UI
.setMouseButton(1);
422 // Hide the controlbar after 2 seconds
423 UI
.closeControlbarTimeout
= setTimeout(UI
.closeControlbar
, 2000);
425 UI
.enableSetting('encrypt');
426 UI
.enableSetting('shared');
427 UI
.enableSetting('host');
428 UI
.enableSetting('port');
429 UI
.enableSetting('path');
430 UI
.enableSetting('repeaterID');
431 UI
.updatePowerButton();
435 // State change closes the password dialog
436 document
.getElementById('noVNC_password_dlg')
437 .classList
.remove('noVNC_open');
440 showStatus(text
, status_type
, time
) {
441 const statusElem
= document
.getElementById('noVNC_status');
443 clearTimeout(UI
.statusTimeout
);
445 if (typeof status_type
=== 'undefined') {
446 status_type
= 'normal';
449 // Don't overwrite more severe visible statuses and never
450 // errors. Only shows the first error.
451 let visible_status_type
= 'none';
452 if (statusElem
.classList
.contains("noVNC_open")) {
453 if (statusElem
.classList
.contains("noVNC_status_error")) {
454 visible_status_type
= 'error';
455 } else if (statusElem
.classList
.contains("noVNC_status_warn")) {
456 visible_status_type
= 'warn';
458 visible_status_type
= 'normal';
461 if (visible_status_type
=== 'error' ||
462 (visible_status_type
=== 'warn' && status_type
=== 'normal')) {
466 switch (status_type
) {
468 statusElem
.classList
.remove("noVNC_status_warn");
469 statusElem
.classList
.remove("noVNC_status_normal");
470 statusElem
.classList
.add("noVNC_status_error");
474 statusElem
.classList
.remove("noVNC_status_error");
475 statusElem
.classList
.remove("noVNC_status_normal");
476 statusElem
.classList
.add("noVNC_status_warn");
481 statusElem
.classList
.remove("noVNC_status_error");
482 statusElem
.classList
.remove("noVNC_status_warn");
483 statusElem
.classList
.add("noVNC_status_normal");
487 statusElem
.textContent
= text
;
488 statusElem
.classList
.add("noVNC_open");
490 // If no time was specified, show the status for 1.5 seconds
491 if (typeof time
=== 'undefined') {
495 // Error messages do not timeout
496 if (status_type
!== 'error') {
497 UI
.statusTimeout
= window
.setTimeout(UI
.hideStatus
, time
);
502 clearTimeout(UI
.statusTimeout
);
503 document
.getElementById('noVNC_status').classList
.remove("noVNC_open");
506 activateControlbar(event
) {
507 clearTimeout(UI
.idleControlbarTimeout
);
508 // We manipulate the anchor instead of the actual control
509 // bar in order to avoid creating new a stacking group
510 document
.getElementById('noVNC_control_bar_anchor')
511 .classList
.remove("noVNC_idle");
512 UI
.idleControlbarTimeout
= window
.setTimeout(UI
.idleControlbar
, 2000);
516 document
.getElementById('noVNC_control_bar_anchor')
517 .classList
.add("noVNC_idle");
521 clearTimeout(UI
.closeControlbarTimeout
);
525 document
.getElementById('noVNC_control_bar')
526 .classList
.add("noVNC_open");
531 document
.getElementById('noVNC_control_bar')
532 .classList
.remove("noVNC_open");
536 if (document
.getElementById('noVNC_control_bar')
537 .classList
.contains("noVNC_open")) {
538 UI
.closeControlbar();
544 toggleControlbarSide() {
545 // Temporarily disable animation, if bar is displayed, to avoid weird
546 // movement. The transitionend-event will not fire when display=none.
547 const bar
= document
.getElementById('noVNC_control_bar');
548 const barDisplayStyle
= window
.getComputedStyle(bar
).display
;
549 if (barDisplayStyle
!== 'none') {
550 bar
.style
.transitionDuration
= '0s';
551 bar
.addEventListener('transitionend', () => bar
.style
.transitionDuration
= '');
554 const anchor
= document
.getElementById('noVNC_control_bar_anchor');
555 if (anchor
.classList
.contains("noVNC_right")) {
556 WebUtil
.writeSetting('controlbar_pos', 'left');
557 anchor
.classList
.remove("noVNC_right");
559 WebUtil
.writeSetting('controlbar_pos', 'right');
560 anchor
.classList
.add("noVNC_right");
563 // Consider this a movement of the handle
564 UI
.controlbarDrag
= true;
567 showControlbarHint(show
) {
568 const hint
= document
.getElementById('noVNC_control_bar_hint');
570 hint
.classList
.add("noVNC_active");
572 hint
.classList
.remove("noVNC_active");
576 dragControlbarHandle(e
) {
577 if (!UI
.controlbarGrabbed
) return;
579 const ptr
= getPointerEvent(e
);
581 const anchor
= document
.getElementById('noVNC_control_bar_anchor');
582 if (ptr
.clientX
< (window
.innerWidth
* 0.1)) {
583 if (anchor
.classList
.contains("noVNC_right")) {
584 UI
.toggleControlbarSide();
586 } else if (ptr
.clientX
> (window
.innerWidth
* 0.9)) {
587 if (!anchor
.classList
.contains("noVNC_right")) {
588 UI
.toggleControlbarSide();
592 if (!UI
.controlbarDrag
) {
593 const dragDistance
= Math
.abs(ptr
.clientY
- UI
.controlbarMouseDownClientY
);
595 if (dragDistance
< dragThreshold
) return;
597 UI
.controlbarDrag
= true;
600 const eventY
= ptr
.clientY
- UI
.controlbarMouseDownOffsetY
;
602 UI
.moveControlbarHandle(eventY
);
607 UI
.activateControlbar();
610 // Move the handle but don't allow any position outside the bounds
611 moveControlbarHandle(viewportRelativeY
) {
612 const handle
= document
.getElementById("noVNC_control_bar_handle");
613 const handleHeight
= handle
.getBoundingClientRect().height
;
614 const controlbarBounds
= document
.getElementById("noVNC_control_bar")
615 .getBoundingClientRect();
618 // These heights need to be non-zero for the below logic to work
619 if (handleHeight
=== 0 || controlbarBounds
.height
=== 0) {
623 let newY
= viewportRelativeY
;
625 // Check if the coordinates are outside the control bar
626 if (newY
< controlbarBounds
.top
+ margin
) {
627 // Force coordinates to be below the top of the control bar
628 newY
= controlbarBounds
.top
+ margin
;
630 } else if (newY
> controlbarBounds
.top
+
631 controlbarBounds
.height
- handleHeight
- margin
) {
632 // Force coordinates to be above the bottom of the control bar
633 newY
= controlbarBounds
.top
+
634 controlbarBounds
.height
- handleHeight
- margin
;
637 // Corner case: control bar too small for stable position
638 if (controlbarBounds
.height
< (handleHeight
+ margin
* 2)) {
639 newY
= controlbarBounds
.top
+
640 (controlbarBounds
.height
- handleHeight
) / 2;
643 // The transform needs coordinates that are relative to the parent
644 const parentRelativeY
= newY
- controlbarBounds
.top
;
645 handle
.style
.transform
= "translateY(" + parentRelativeY
+ "px)";
648 updateControlbarHandle() {
649 // Since the control bar is fixed on the viewport and not the page,
650 // the move function expects coordinates relative the the viewport.
651 const handle
= document
.getElementById("noVNC_control_bar_handle");
652 const handleBounds
= handle
.getBoundingClientRect();
653 UI
.moveControlbarHandle(handleBounds
.top
);
656 controlbarHandleMouseUp(e
) {
657 if ((e
.type
== "mouseup") && (e
.button
!= 0)) return;
659 // mouseup and mousedown on the same place toggles the controlbar
660 if (UI
.controlbarGrabbed
&& !UI
.controlbarDrag
) {
661 UI
.toggleControlbar();
665 UI
.activateControlbar();
667 UI
.controlbarGrabbed
= false;
668 UI
.showControlbarHint(false);
671 controlbarHandleMouseDown(e
) {
672 if ((e
.type
== "mousedown") && (e
.button
!= 0)) return;
674 const ptr
= getPointerEvent(e
);
676 const handle
= document
.getElementById("noVNC_control_bar_handle");
677 const bounds
= handle
.getBoundingClientRect();
679 // Touch events have implicit capture
680 if (e
.type
=== "mousedown") {
684 UI
.controlbarGrabbed
= true;
685 UI
.controlbarDrag
= false;
687 UI
.showControlbarHint(true);
689 UI
.controlbarMouseDownClientY
= ptr
.clientY
;
690 UI
.controlbarMouseDownOffsetY
= ptr
.clientY
- bounds
.top
;
694 UI
.activateControlbar();
698 if (this.classList
.contains("noVNC_open")) {
699 this.classList
.remove("noVNC_open");
701 this.classList
.add("noVNC_open");
711 // Initial page load read/initialization of settings
712 initSetting(name
, defVal
) {
713 // Check Query string followed by cookie
714 let val
= WebUtil
.getConfigVar(name
);
716 val
= WebUtil
.readSetting(name
, defVal
);
718 WebUtil
.setSetting(name
, val
);
719 UI
.updateSetting(name
);
723 // Set the new value, update and disable form control setting
724 forceSetting(name
, val
) {
725 WebUtil
.setSetting(name
, val
);
726 UI
.updateSetting(name
);
727 UI
.disableSetting(name
);
730 // Update cookie and form control setting. If value is not set, then
731 // updates from control to current cookie setting.
732 updateSetting(name
) {
734 // Update the settings control
735 let value
= UI
.getSetting(name
);
737 const ctrl
= document
.getElementById('noVNC_setting_' + name
);
738 if (ctrl
.type
=== 'checkbox') {
739 ctrl
.checked
= value
;
741 } else if (typeof ctrl
.options
!== 'undefined') {
742 for (let i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
743 if (ctrl
.options
[i
].value
=== value
) {
744 ctrl
.selectedIndex
= i
;
749 /*Weird IE9 error leads to 'null' appearring
750 in textboxes instead of ''.*/
751 if (value
=== null) {
758 // Save control setting to cookie
760 const ctrl
= document
.getElementById('noVNC_setting_' + name
);
762 if (ctrl
.type
=== 'checkbox') {
764 } else if (typeof ctrl
.options
!== 'undefined') {
765 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
769 WebUtil
.writeSetting(name
, val
);
770 //Log.Debug("Setting saved '" + name + "=" + val + "'");
774 // Read form control compatible setting from cookie
776 const ctrl
= document
.getElementById('noVNC_setting_' + name
);
777 let val
= WebUtil
.readSetting(name
);
778 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
779 if (val
.toString().toLowerCase() in {'0': 1, 'no': 1, 'false': 1}) {
788 // These helpers compensate for the lack of parent-selectors and
789 // previous-sibling-selectors in CSS which are needed when we want to
790 // disable the labels that belong to disabled input elements.
791 disableSetting(name
) {
792 const ctrl
= document
.getElementById('noVNC_setting_' + name
);
793 ctrl
.disabled
= true;
794 ctrl
.label
.classList
.add('noVNC_disabled');
797 enableSetting(name
) {
798 const ctrl
= document
.getElementById('noVNC_setting_' + name
);
799 ctrl
.disabled
= false;
800 ctrl
.label
.classList
.remove('noVNC_disabled');
810 UI
.closeSettingsPanel();
811 UI
.closePowerPanel();
812 UI
.closeClipboardPanel();
822 openSettingsPanel() {
826 // Refresh UI elements from saved cookies
827 UI
.updateSetting('encrypt');
828 UI
.updateSetting('view_clip');
829 UI
.updateSetting('resize');
830 UI
.updateSetting('shared');
831 UI
.updateSetting('view_only');
832 UI
.updateSetting('path');
833 UI
.updateSetting('repeaterID');
834 UI
.updateSetting('logging');
835 UI
.updateSetting('reconnect');
836 UI
.updateSetting('reconnect_delay');
838 document
.getElementById('noVNC_settings')
839 .classList
.add("noVNC_open");
840 document
.getElementById('noVNC_settings_button')
841 .classList
.add("noVNC_selected");
844 closeSettingsPanel() {
845 document
.getElementById('noVNC_settings')
846 .classList
.remove("noVNC_open");
847 document
.getElementById('noVNC_settings_button')
848 .classList
.remove("noVNC_selected");
851 toggleSettingsPanel() {
852 if (document
.getElementById('noVNC_settings')
853 .classList
.contains("noVNC_open")) {
854 UI
.closeSettingsPanel();
856 UI
.openSettingsPanel();
870 document
.getElementById('noVNC_power')
871 .classList
.add("noVNC_open");
872 document
.getElementById('noVNC_power_button')
873 .classList
.add("noVNC_selected");
877 document
.getElementById('noVNC_power')
878 .classList
.remove("noVNC_open");
879 document
.getElementById('noVNC_power_button')
880 .classList
.remove("noVNC_selected");
884 if (document
.getElementById('noVNC_power')
885 .classList
.contains("noVNC_open")) {
886 UI
.closePowerPanel();
892 // Disable/enable power button
893 updatePowerButton() {
895 UI
.rfb
.capabilities
.power
&&
897 document
.getElementById('noVNC_power_button')
898 .classList
.remove("noVNC_hidden");
900 document
.getElementById('noVNC_power_button')
901 .classList
.add("noVNC_hidden");
902 // Close power panel if open
903 UI
.closePowerPanel();
913 openClipboardPanel() {
917 document
.getElementById('noVNC_clipboard')
918 .classList
.add("noVNC_open");
919 document
.getElementById('noVNC_clipboard_button')
920 .classList
.add("noVNC_selected");
923 closeClipboardPanel() {
924 document
.getElementById('noVNC_clipboard')
925 .classList
.remove("noVNC_open");
926 document
.getElementById('noVNC_clipboard_button')
927 .classList
.remove("noVNC_selected");
930 toggleClipboardPanel() {
931 if (document
.getElementById('noVNC_clipboard')
932 .classList
.contains("noVNC_open")) {
933 UI
.closeClipboardPanel();
935 UI
.openClipboardPanel();
939 clipboardReceive(e
) {
940 Log
.Debug(">> UI.clipboardReceive: " + e
.detail
.text
.substr(0, 40) + "...");
941 document
.getElementById('noVNC_clipboard_text').value
= e
.detail
.text
;
942 Log
.Debug("<< UI.clipboardReceive");
946 document
.getElementById('noVNC_clipboard_text').value
= "";
947 UI
.rfb
.clipboardPasteFrom("");
951 const text
= document
.getElementById('noVNC_clipboard_text').value
;
952 Log
.Debug(">> UI.clipboardSend: " + text
.substr(0, 40) + "...");
953 UI
.rfb
.clipboardPasteFrom(text
);
954 Log
.Debug("<< UI.clipboardSend");
964 document
.getElementById('noVNC_connect_dlg')
965 .classList
.add("noVNC_open");
968 closeConnectPanel() {
969 document
.getElementById('noVNC_connect_dlg')
970 .classList
.remove("noVNC_open");
973 connect(event
, password
) {
975 // Ignore when rfb already exists
976 if (typeof UI
.rfb
!== 'undefined') {
980 const host
= UI
.getSetting('host');
981 const port
= UI
.getSetting('port');
982 const path
= UI
.getSetting('path');
984 if (typeof password
=== 'undefined') {
985 password
= WebUtil
.getConfigVar('password');
986 UI
.reconnect_password
= password
;
989 if (password
=== null) {
990 password
= undefined;
996 Log
.Error("Can't connect when host is: " + host
);
997 UI
.showStatus(_("Must set host"), 'error');
1001 UI
.closeAllPanels();
1002 UI
.closeConnectPanel();
1004 UI
.updateVisualState('connecting');
1008 url
= UI
.getSetting('encrypt') ? 'wss' : 'ws';
1010 url
+= '://' + host
;
1016 UI
.rfb
= new RFB(document
.getElementById('noVNC_container'), url
,
1017 { shared
: UI
.getSetting('shared'),
1018 showDotCursor
: UI
.getSetting('show_dot'),
1019 repeaterID
: UI
.getSetting('repeaterID'),
1020 credentials
: { password
: password
} });
1021 UI
.rfb
.addEventListener("connect", UI
.connectFinished
);
1022 UI
.rfb
.addEventListener("disconnect", UI
.disconnectFinished
);
1023 UI
.rfb
.addEventListener("credentialsrequired", UI
.credentials
);
1024 UI
.rfb
.addEventListener("securityfailure", UI
.securityFailed
);
1025 UI
.rfb
.addEventListener("capabilities", UI
.updatePowerButton
);
1026 UI
.rfb
.addEventListener("clipboard", UI
.clipboardReceive
);
1027 UI
.rfb
.addEventListener("bell", UI
.bell
);
1028 UI
.rfb
.addEventListener("desktopname", UI
.updateDesktopName
);
1029 UI
.rfb
.clipViewport
= UI
.getSetting('view_clip');
1030 UI
.rfb
.scaleViewport
= UI
.getSetting('resize') === 'scale';
1031 UI
.rfb
.resizeSession
= UI
.getSetting('resize') === 'remote';
1033 UI
.updateViewOnly(); // requires UI.rfb
1037 UI
.closeAllPanels();
1038 UI
.rfb
.disconnect();
1040 UI
.connected
= false;
1042 // Disable automatic reconnecting
1043 UI
.inhibit_reconnect
= true;
1045 UI
.updateVisualState('disconnecting');
1047 // Don't display the connection settings until we're actually disconnected
1051 UI
.reconnect_callback
= null;
1053 // if reconnect has been disabled in the meantime, do nothing.
1054 if (UI
.inhibit_reconnect
) {
1058 UI
.connect(null, UI
.reconnect_password
);
1062 if (UI
.reconnect_callback
!== null) {
1063 clearTimeout(UI
.reconnect_callback
);
1064 UI
.reconnect_callback
= null;
1067 UI
.updateVisualState('disconnected');
1069 UI
.openControlbar();
1070 UI
.openConnectPanel();
1073 connectFinished(e
) {
1074 UI
.connected
= true;
1075 UI
.inhibit_reconnect
= false;
1078 if (UI
.getSetting('encrypt')) {
1079 msg
= _("Connected (encrypted) to ") + UI
.desktopName
;
1081 msg
= _("Connected (unencrypted) to ") + UI
.desktopName
;
1084 UI
.updateVisualState('connected');
1086 // Do this last because it can only be used on rendered elements
1090 disconnectFinished(e
) {
1091 const wasConnected
= UI
.connected
;
1093 // This variable is ideally set when disconnection starts, but
1094 // when the disconnection isn't clean or if it is initiated by
1095 // the server, we need to do it here as well since
1096 // UI.disconnect() won't be used in those cases.
1097 UI
.connected
= false;
1101 if (!e
.detail
.clean
) {
1102 UI
.updateVisualState('disconnected');
1104 UI
.showStatus(_("Something went wrong, connection is closed"),
1107 UI
.showStatus(_("Failed to connect to server"), 'error');
1109 } else if (UI
.getSetting('reconnect', false) === true && !UI
.inhibit_reconnect
) {
1110 UI
.updateVisualState('reconnecting');
1112 const delay
= parseInt(UI
.getSetting('reconnect_delay'));
1113 UI
.reconnect_callback
= setTimeout(UI
.reconnect
, delay
);
1116 UI
.updateVisualState('disconnected');
1117 UI
.showStatus(_("Disconnected"), 'normal');
1120 UI
.openControlbar();
1121 UI
.openConnectPanel();
1126 // On security failures we might get a string with a reason
1127 // directly from the server. Note that we can't control if
1128 // this string is translated or not.
1129 if ('reason' in e
.detail
) {
1130 msg
= _("New connection has been rejected with reason: ") +
1133 msg
= _("New connection has been rejected");
1135 UI
.showStatus(msg
, 'error');
1145 // FIXME: handle more types
1146 document
.getElementById('noVNC_password_dlg')
1147 .classList
.add('noVNC_open');
1149 setTimeout(() => document
1150 .getElementById('noVNC_password_input').focus(), 100);
1152 Log
.Warn("Server asked for a password");
1153 UI
.showStatus(_("Password is required"), "warning");
1157 // Prevent actually submitting the form
1160 const inputElem
= document
.getElementById('noVNC_password_input');
1161 const password
= inputElem
.value
;
1162 // Clear the input after reading the password
1163 inputElem
.value
= "";
1164 UI
.rfb
.sendCredentials({ password
: password
});
1165 UI
.reconnect_password
= password
;
1166 document
.getElementById('noVNC_password_dlg')
1167 .classList
.remove('noVNC_open');
1176 toggleFullscreen() {
1177 if (document
.fullscreenElement
|| // alternative standard method
1178 document
.mozFullScreenElement
|| // currently working methods
1179 document
.webkitFullscreenElement
||
1180 document
.msFullscreenElement
) {
1181 if (document
.exitFullscreen
) {
1182 document
.exitFullscreen();
1183 } else if (document
.mozCancelFullScreen
) {
1184 document
.mozCancelFullScreen();
1185 } else if (document
.webkitExitFullscreen
) {
1186 document
.webkitExitFullscreen();
1187 } else if (document
.msExitFullscreen
) {
1188 document
.msExitFullscreen();
1191 if (document
.documentElement
.requestFullscreen
) {
1192 document
.documentElement
.requestFullscreen();
1193 } else if (document
.documentElement
.mozRequestFullScreen
) {
1194 document
.documentElement
.mozRequestFullScreen();
1195 } else if (document
.documentElement
.webkitRequestFullscreen
) {
1196 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
1197 } else if (document
.body
.msRequestFullscreen
) {
1198 document
.body
.msRequestFullscreen();
1201 UI
.updateFullscreenButton();
1204 updateFullscreenButton() {
1205 if (document
.fullscreenElement
|| // alternative standard method
1206 document
.mozFullScreenElement
|| // currently working methods
1207 document
.webkitFullscreenElement
||
1208 document
.msFullscreenElement
) {
1209 document
.getElementById('noVNC_fullscreen_button')
1210 .classList
.add("noVNC_selected");
1212 document
.getElementById('noVNC_fullscreen_button')
1213 .classList
.remove("noVNC_selected");
1223 // Apply remote resizing or local scaling
1225 if (!UI
.rfb
) return;
1227 UI
.rfb
.scaleViewport
= UI
.getSetting('resize') === 'scale';
1228 UI
.rfb
.resizeSession
= UI
.getSetting('resize') === 'remote';
1237 // Update viewport clipping property for the connection. The normal
1238 // case is to get the value from the setting. There are special cases
1239 // for when the viewport is scaled or when a touch device is used.
1241 if (!UI
.rfb
) return;
1243 const scaling
= UI
.getSetting('resize') === 'scale';
1246 // Can't be clipping if viewport is scaled to fit
1247 UI
.forceSetting('view_clip', false);
1248 UI
.rfb
.clipViewport
= false;
1249 } else if (isTouchDevice
) {
1250 // Touch devices usually have shit scrollbars
1251 UI
.forceSetting('view_clip', true);
1252 UI
.rfb
.clipViewport
= true;
1254 UI
.enableSetting('view_clip');
1255 UI
.rfb
.clipViewport
= UI
.getSetting('view_clip');
1258 // Changing the viewport may change the state of
1259 // the dragging button
1260 UI
.updateViewDrag();
1270 if (!UI
.rfb
) return;
1272 UI
.rfb
.dragViewport
= !UI
.rfb
.dragViewport
;
1273 UI
.updateViewDrag();
1277 if (!UI
.connected
) return;
1279 const viewDragButton
= document
.getElementById('noVNC_view_drag_button');
1281 if (!UI
.rfb
.clipViewport
&& UI
.rfb
.dragViewport
) {
1282 // We are no longer clipping the viewport. Make sure
1283 // viewport drag isn't active when it can't be used.
1284 UI
.rfb
.dragViewport
= false;
1287 if (UI
.rfb
.dragViewport
) {
1288 viewDragButton
.classList
.add("noVNC_selected");
1290 viewDragButton
.classList
.remove("noVNC_selected");
1293 // Different behaviour for touch vs non-touch
1294 // The button is disabled instead of hidden on touch devices
1295 if (isTouchDevice
) {
1296 viewDragButton
.classList
.remove("noVNC_hidden");
1298 if (UI
.rfb
.clipViewport
) {
1299 viewDragButton
.disabled
= false;
1301 viewDragButton
.disabled
= true;
1304 viewDragButton
.disabled
= false;
1306 if (UI
.rfb
.clipViewport
) {
1307 viewDragButton
.classList
.remove("noVNC_hidden");
1309 viewDragButton
.classList
.add("noVNC_hidden");
1320 showVirtualKeyboard() {
1321 if (!isTouchDevice
) return;
1323 const input
= document
.getElementById('noVNC_keyboardinput');
1325 if (document
.activeElement
== input
) return;
1330 const l
= input
.value
.length
;
1331 // Move the caret to the end
1332 input
.setSelectionRange(l
, l
);
1334 // setSelectionRange is undefined in Google Chrome
1338 hideVirtualKeyboard() {
1339 if (!isTouchDevice
) return;
1341 const input
= document
.getElementById('noVNC_keyboardinput');
1343 if (document
.activeElement
!= input
) return;
1348 toggleVirtualKeyboard() {
1349 if (document
.getElementById('noVNC_keyboard_button')
1350 .classList
.contains("noVNC_selected")) {
1351 UI
.hideVirtualKeyboard();
1353 UI
.showVirtualKeyboard();
1357 onfocusVirtualKeyboard(event
) {
1358 document
.getElementById('noVNC_keyboard_button')
1359 .classList
.add("noVNC_selected");
1361 UI
.rfb
.focusOnClick
= false;
1365 onblurVirtualKeyboard(event
) {
1366 document
.getElementById('noVNC_keyboard_button')
1367 .classList
.remove("noVNC_selected");
1369 UI
.rfb
.focusOnClick
= true;
1373 keepVirtualKeyboard(event
) {
1374 const input
= document
.getElementById('noVNC_keyboardinput');
1376 // Only prevent focus change if the virtual keyboard is active
1377 if (document
.activeElement
!= input
) {
1381 // Only allow focus to move to other elements that need
1382 // focus to function properly
1383 if (event
.target
.form
!== undefined) {
1384 switch (event
.target
.type
) {
1393 case 'select-multiple':
1398 event
.preventDefault();
1401 keyboardinputReset() {
1402 const kbi
= document
.getElementById('noVNC_keyboardinput');
1403 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1404 UI
.lastKeyboardinput
= kbi
.value
;
1407 keyEvent(keysym
, code
, down
) {
1408 if (!UI
.rfb
) return;
1410 UI
.rfb
.sendKey(keysym
, code
, down
);
1413 // When normal keyboard events are left uncought, use the input events from
1414 // the keyboardinput element instead and generate the corresponding key events.
1415 // This code is required since some browsers on Android are inconsistent in
1416 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1419 if (!UI
.rfb
) return;
1421 const newValue
= event
.target
.value
;
1423 if (!UI
.lastKeyboardinput
) {
1424 UI
.keyboardinputReset();
1426 const oldValue
= UI
.lastKeyboardinput
;
1430 // Try to check caret position since whitespace at the end
1431 // will not be considered by value.length in some browsers
1432 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1434 // selectionStart is undefined in Google Chrome
1435 newLen
= newValue
.length
;
1437 const oldLen
= oldValue
.length
;
1439 let inputs
= newLen
- oldLen
;
1440 let backspaces
= inputs
< 0 ? -inputs
: 0;
1442 // Compare the old string with the new to account for
1443 // text-corrections or other input that modify existing text
1444 for (let i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1445 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1446 inputs
= newLen
- i
;
1447 backspaces
= oldLen
- i
;
1452 // Send the key events
1453 for (let i
= 0; i
< backspaces
; i
++) {
1454 UI
.rfb
.sendKey(KeyTable
.XK_BackSpace
, "Backspace");
1456 for (let i
= newLen
- inputs
; i
< newLen
; i
++) {
1457 UI
.rfb
.sendKey(keysyms
.lookup(newValue
.charCodeAt(i
)));
1460 // Control the text content length in the keyboardinput element
1461 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1462 UI
.keyboardinputReset();
1463 } else if (newLen
< 1) {
1464 // There always have to be some text in the keyboardinput
1465 // element with which backspace can interact.
1466 UI
.keyboardinputReset();
1467 // This sometimes causes the keyboard to disappear for a second
1468 // but it is required for the android keyboard to recognize that
1469 // text has been added to the field
1470 event
.target
.blur();
1471 // This has to be ran outside of the input handler in order to work
1472 setTimeout(event
.target
.focus
.bind(event
.target
), 0);
1474 UI
.lastKeyboardinput
= newValue
;
1485 UI
.closeAllPanels();
1486 UI
.openControlbar();
1488 document
.getElementById('noVNC_modifiers')
1489 .classList
.add("noVNC_open");
1490 document
.getElementById('noVNC_toggle_extra_keys_button')
1491 .classList
.add("noVNC_selected");
1495 document
.getElementById('noVNC_modifiers')
1496 .classList
.remove("noVNC_open");
1497 document
.getElementById('noVNC_toggle_extra_keys_button')
1498 .classList
.remove("noVNC_selected");
1502 if (document
.getElementById('noVNC_modifiers')
1503 .classList
.contains("noVNC_open")) {
1504 UI
.closeExtraKeys();
1511 UI
.rfb
.sendKey(KeyTable
.XK_Escape
, "Escape");
1515 UI
.rfb
.sendKey(KeyTable
.XK_Tab
);
1519 const btn
= document
.getElementById('noVNC_toggle_ctrl_button');
1520 if (btn
.classList
.contains("noVNC_selected")) {
1521 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, "ControlLeft", false);
1522 btn
.classList
.remove("noVNC_selected");
1524 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, "ControlLeft", true);
1525 btn
.classList
.add("noVNC_selected");
1530 const btn
= document
.getElementById('noVNC_toggle_alt_button');
1531 if (btn
.classList
.contains("noVNC_selected")) {
1532 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, "AltLeft", false);
1533 btn
.classList
.remove("noVNC_selected");
1535 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, "AltLeft", true);
1536 btn
.classList
.add("noVNC_selected");
1541 UI
.rfb
.sendCtrlAltDel();
1550 setMouseButton(num
) {
1551 const view_only
= UI
.rfb
.viewOnly
;
1552 if (UI
.rfb
&& !view_only
) {
1553 UI
.rfb
.touchButton
= num
;
1556 const blist
= [0, 1, 2, 4];
1557 for (let b
= 0; b
< blist
.length
; b
++) {
1558 const button
= document
.getElementById('noVNC_mouse_button' +
1560 if (blist
[b
] === num
&& !view_only
) {
1561 button
.classList
.remove("noVNC_hidden");
1563 button
.classList
.add("noVNC_hidden");
1569 if (!UI
.rfb
) return;
1570 UI
.rfb
.viewOnly
= UI
.getSetting('view_only');
1572 // Hide input related buttons in view only mode
1573 if (UI
.rfb
.viewOnly
) {
1574 document
.getElementById('noVNC_keyboard_button')
1575 .classList
.add('noVNC_hidden');
1576 document
.getElementById('noVNC_toggle_extra_keys_button')
1577 .classList
.add('noVNC_hidden');
1579 document
.getElementById('noVNC_keyboard_button')
1580 .classList
.remove('noVNC_hidden');
1581 document
.getElementById('noVNC_toggle_extra_keys_button')
1582 .classList
.remove('noVNC_hidden');
1584 UI
.setMouseButton(1); //has it's own logic for hiding/showing
1587 updateShowDotCursor() {
1588 if (!UI
.rfb
) return;
1589 UI
.rfb
.showDotCursor
= UI
.getSetting('show_dot');
1593 WebUtil
.init_logging(UI
.getSetting('logging'));
1596 updateDesktopName(e
) {
1597 UI
.desktopName
= e
.detail
.name
;
1598 // Display the desktop name in the document title
1599 document
.title
= e
.detail
.name
+ " - noVNC";
1603 if (WebUtil
.getConfigVar('bell', 'on') === 'on') {
1604 const promise
= document
.getElementById('noVNC_bell').play();
1605 // The standards disagree on the return value here
1607 promise
.catch((e
) => {
1608 if (e
.name
=== "NotAllowedError") {
1609 // Ignore when the browser doesn't let us play audio.
1610 // It is common that the browsers require audio to be
1611 // initiated from a user action.
1613 Log
.Error("Unable to play bell: " + e
);
1620 //Helper to add options to dropdown.
1621 addOption(selectbox
, text
, value
) {
1622 const optn
= document
.createElement("OPTION");
1625 selectbox
.options
.add(optn
);
1634 // Set up translations
1635 const LINGUAS
= ["de", "el", "es", "nl", "pl", "sv", "tr", "zh_CN", "zh_TW"];
1636 l10n
.setup(LINGUAS
);
1637 if (l10n
.language
!== "en" && l10n
.dictionary
=== undefined) {
1638 WebUtil
.fetchJSON('app/locale/' + l10n
.language
+ '.json', (translations
) => {
1639 l10n
.dictionary
= translations
;
1641 // wait for translations to load before loading the UI
1644 Log
.Error("Failed to load translations: " + err
);