Skip to content

Commit

Permalink
feat: Set onbeforeunload to avoid losing the current terminal session…
Browse files Browse the repository at this point in the history
… with Ctrl+W (#23)

Co-authored-by: Filip Troníček <[email protected]>
  • Loading branch information
erikmd and filiptronicek authored Oct 29, 2024
1 parent 48b1956 commit d547d4f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/lib/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d547d4f

Please sign in to comment.