]> git.proxmox.com Git - pve-xtermjs.git/commitdiff
ui: prevent accidental closing of terminal window
authorDominik Csapak <d.csapak@proxmox.com>
Tue, 21 Jul 2020 09:00:48 +0000 (11:00 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 23 Jul 2020 10:13:31 +0000 (12:13 +0200)
this prevents closing the window e.g. when pressing ctrl+w

most browsers have a function to block a website from doing that,
so users that do not want that, simply have to click that option

also, the message will (at least in chrome) not be displayed,
instead a fixed message will appear

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
src/www/main.js

index bb71812b3ada042993bc5fc5ea2b8c6aaadd11c3..39aef11a3a98f56608f70857222a29d6bf1fc53c 100644 (file)
@@ -35,6 +35,7 @@ function updateState(newState, msg, code) {
            severity = severities.warning;
            break;
        case states.connected:
+           window.onbeforeunload = windowUnload;
            message = "Connected";
            break;
        case states.disconnecting:
@@ -48,6 +49,7 @@ function updateState(newState, msg, code) {
            severity = severities.warning;
            break;
        case states.disconnected:
+           window.onbeforeunload = undefined;
            switch (state) {
                case states.start:
                case states.connecting:
@@ -143,7 +145,6 @@ function createTerminal() {
            socket.onopen = runTerminal;
            socket.onclose = tryReconnect;
            socket.onerror = tryReconnect;
-           window.onbeforeunload = stopTerminal;
            updateState(states.connecting);
        },
        failure: function(msg) {
@@ -303,6 +304,17 @@ function clearEvents() {
     term.onData(() => {});
 }
 
+function windowUnload(e) {
+    let message = "Are you sure you want to leave this page?";
+
+    e = e || window.event;
+    if (e) {
+       e.returnValue = message;
+    }
+
+    return message;
+}
+
 function stopTerminal(event) {
     event = event || {};
     clearEvents();