Display warning prompt before closing tab

The warning prompt is only displayed if there is an active connected
session, when viewOnly is disabled.

(cherry picked from commit 63c2c14a50)
This commit is contained in:
Tobias
2025-10-27 17:22:59 +01:00
committed by Alexander Zeijlon
parent f6a4d0a50a
commit 8823149810

View File

@@ -1154,6 +1154,8 @@ const UI = {
UI.showStatus(msg);
UI.updateVisualState('connected');
UI.updateBeforeUnload();
// Do this last because it can only be used on rendered elements
UI.rfb.focus();
},
@@ -1190,6 +1192,8 @@ const UI = {
UI.showStatus(_("Disconnected"), 'normal');
}
UI.updateBeforeUnload();
document.title = PAGE_TITLE;
UI.openControlbar();
@@ -1210,6 +1214,24 @@ const UI = {
UI.showStatus(msg, 'error');
},
handleBeforeUnload(e) {
// Trigger a "Leave site?" warning prompt before closing the
// page. Modern browsers (Oct 2025) accept either (or both)
// preventDefault() or a nonempty returnValue, though the latter is
// considered legacy. The custom string is ignored by modern browsers,
// which display a native message, but older browsers will show it.
e.preventDefault();
e.returnValue = _("Are you sure you want to disconnect the session?");
},
updateBeforeUnload() {
// Remove first to avoid adding duplicates
window.removeEventListener("beforeunload", UI.handleBeforeUnload);
if (!UI.rfb?.viewOnly && UI.connected) {
window.addEventListener("beforeunload", UI.handleBeforeUnload);
}
},
/* ------^-------
* /CONNECTION
* ==============
@@ -1736,6 +1758,8 @@ const UI = {
if (!UI.rfb) return;
UI.rfb.viewOnly = UI.getSetting('view_only');
UI.updateBeforeUnload();
// Hide input related buttons in view only mode
if (UI.rfb.viewOnly) {
document.getElementById('noVNC_keyboard_button')