[pbs-devel] [PATCH xtermjs v3 4/4] ui: prevent accidental closing of terminal window

Dominik Csapak d.csapak at proxmox.com
Tue Jul 21 11:00:48 CEST 2020


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 at proxmox.com>
---
 src/www/main.js | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/www/main.js b/src/www/main.js
index bb71812..39aef11 100644
--- a/src/www/main.js
+++ b/src/www/main.js
@@ -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();
-- 
2.20.1






More information about the pbs-devel mailing list