From 679b0e98b3f16262c24aaa102dd3794e71a55094 Mon Sep 17 00:00:00 2001 From: Erik Martin-Dorel Date: Sat, 14 Sep 2024 22:35:12 +0200 Subject: [PATCH 1/2] feat: Set onbeforeunload to avoid losing the curr. terminal session with Ctrl+W Close gitpod-io/gitpod#18416 --- src/client.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/client.ts b/src/client.ts index 7737bf2..36f0aa3 100644 --- a/src/client.ts +++ b/src/client.ts @@ -205,6 +205,12 @@ async function createTerminal( const debouncedUpdateTerminalSize = debounce(() => updateTerminalSize(term), 200, { trailing: true }); window.onresize = () => debouncedUpdateTerminalSize(); + // Ask for confirmation before closing the current terminal session + window.onbeforeunload = (event) => { + event.preventDefault(); + event.returnValue = "really?"; // supporting legacy browsers + }; + // Register the onclick event for the reconnect button reconnectButton.onclick = () => terminalSocket.reconnect(); From 76eae0c6cf143860a385055bd555b82949f99eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Tron=C3=AD=C4=8Dek?= Date: Tue, 29 Oct 2024 11:58:55 +0000 Subject: [PATCH 2/2] Introduce toggling with `XTERM_CONFIRM_BROWSER_EXIT` --- server.cjs | 10 +++++++++- src/client.ts | 6 ------ src/lib/remote.ts | 7 +++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/server.cjs b/server.cjs index 82021ad..fcd4f33 100644 --- a/server.cjs +++ b/server.cjs @@ -182,7 +182,15 @@ function startServer() { } } - sendPortUpdates(); + async function init() { + if (process.env["XTERM_CONFIRM_BROWSER_EXIT"] === "true") { + ws.send(JSON.stringify({ action: "confirmExit" })); + } + + sendPortUpdates(); + } + + init(); }); let clientForExternalMessages = null; diff --git a/src/client.ts b/src/client.ts index 36f0aa3..7737bf2 100644 --- a/src/client.ts +++ b/src/client.ts @@ -205,12 +205,6 @@ async function createTerminal( const debouncedUpdateTerminalSize = debounce(() => updateTerminalSize(term), 200, { trailing: true }); window.onresize = () => debouncedUpdateTerminalSize(); - // Ask for confirmation before closing the current terminal session - window.onbeforeunload = (event) => { - event.preventDefault(); - event.returnValue = "really?"; // supporting legacy browsers - }; - // Register the onclick event for the reconnect button reconnectButton.onclick = () => terminalSocket.reconnect(); diff --git a/src/lib/remote.ts b/src/lib/remote.ts index ee7e588..2c8d5d0 100644 --- a/src/lib/remote.ts +++ b/src/lib/remote.ts @@ -66,6 +66,13 @@ export const initiateRemoteCommunicationChannelSocket = async (protocol: string) output(`Port ${port} has been opened`, { formActions: [openUrlButton], reason: "info" }); break; } + case "confirmExit": { + // Ask for confirmation before closing the current terminal session + window.onbeforeunload = (e: BeforeUnloadEvent) => { + e.preventDefault(); + e.returnValue = "Are you sure you want to close the terminal?"; + }; + } default: console.debug("Unhandled message", messageData); }