Skip to content

Commit

Permalink
chore: user prompt before the mapper tab closed
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed May 2, 2024
1 parent 28ff413 commit dfb98a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/bidiServer/MapperCdpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down
5 changes: 5 additions & 0 deletions src/bidiTab/bidiTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/bidiTab/mapperTabPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit dfb98a0

Please sign in to comment.