Display warning prompt before closing tab

The warning prompt is only displayed if there is an active connected
session, when viewOnly is disabled.
This commit is contained in:
Tobias
2025-10-27 17:22:59 +01:00
parent 91d12106df
commit 63c2c14a50

View File

@@ -1155,6 +1155,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();
},
@@ -1191,6 +1193,8 @@ const UI = {
UI.showStatus(_("Disconnected"), 'normal');
}
UI.updateBeforeUnload();
document.title = PAGE_TITLE;
UI.openControlbar();
@@ -1211,6 +1215,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
* ==============
@@ -1737,6 +1759,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')