Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: user prompt before the mapper tab closed #2182

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading