From dfb98a06d9fc6802f2dffaaef44a2f1bfa749994 Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Thu, 2 May 2024 11:29:11 +0200 Subject: [PATCH] chore: user prompt before the mapper tab closed --- src/bidiServer/MapperCdpConnection.ts | 7 +++++++ src/bidiTab/bidiTab.ts | 5 +++++ src/bidiTab/mapperTabPage.ts | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/src/bidiServer/MapperCdpConnection.ts b/src/bidiServer/MapperCdpConnection.ts index 49c15d15b0..b25146fecb 100644 --- a/src/bidiServer/MapperCdpConnection.ts +++ b/src/bidiServer/MapperCdpConnection.ts @@ -160,6 +160,13 @@ export class MapperServerCdpConnection { const mapperCdpClient = cdpConnection.getCdpClient(mapperSessionId); + // Click on the body to interact with the page in order to "beforeunload" being + // triggered when the tab is closed. + await mapperCdpClient.sendCommand('Runtime.evaluate', { + expression: 'document.body.click()', + userGesture: true, + }); + const bidiSession = new SimpleTransport( async (message) => await this.#sendMessage(mapperCdpClient, message) ); diff --git a/src/bidiTab/bidiTab.ts b/src/bidiTab/bidiTab.ts index c3fa89786d..bea60d4e7e 100644 --- a/src/bidiTab/bidiTab.ts +++ b/src/bidiTab/bidiTab.ts @@ -48,6 +48,11 @@ declare global { // Set from the server side if verbose logging is required. sendDebugMessage?: ((message: string) => void) | null; + + // Required to prevent the user from closing the tab. + onbeforeunload: + | ((this: WindowEventHandlers, ev: BeforeUnloadEvent) => any) + | null; } } diff --git a/src/bidiTab/mapperTabPage.ts b/src/bidiTab/mapperTabPage.ts index ac292d25d5..7c1eda83d8 100644 --- a/src/bidiTab/mapperTabPage.ts +++ b/src/bidiTab/mapperTabPage.ts @@ -26,6 +26,10 @@ export function generatePage() { return; } globalThis.document.documentElement.innerHTML = mapperPageSource; + + // Show a confirmation dialog when the user tries to leave the Mapper tab. + globalThis.window.onbeforeunload = () => + 'Closing or reloading this tab will stop the BiDi process. Are you sure you want to leave?'; } function stringify(message: unknown) {