Skip to content

Commit d547d4f

Browse files
feat: Set onbeforeunload to avoid losing the current terminal session with Ctrl+W (#23)
Co-authored-by: Filip Troníček <[email protected]>
1 parent 48b1956 commit d547d4f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

server.cjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,15 @@ function startServer() {
182182
}
183183
}
184184

185-
sendPortUpdates();
185+
async function init() {
186+
if (process.env["XTERM_CONFIRM_BROWSER_EXIT"] === "true") {
187+
ws.send(JSON.stringify({ action: "confirmExit" }));
188+
}
189+
190+
sendPortUpdates();
191+
}
192+
193+
init();
186194
});
187195

188196
let clientForExternalMessages = null;

src/lib/remote.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ export const initiateRemoteCommunicationChannelSocket = async (protocol: string)
6666
output(`Port ${port} has been opened`, { formActions: [openUrlButton], reason: "info" });
6767
break;
6868
}
69+
case "confirmExit": {
70+
// Ask for confirmation before closing the current terminal session
71+
window.onbeforeunload = (e: BeforeUnloadEvent) => {
72+
e.preventDefault();
73+
e.returnValue = "Are you sure you want to close the terminal?";
74+
};
75+
}
6976
default:
7077
console.debug("Unhandled message", messageData);
7178
}

0 commit comments

Comments
 (0)