Add prevent_unload/allow_unload actions#336
Conversation
There was a problem hiding this comment.
Pull request overview
Adds two TurboPower Browser actions that let the server enable/disable a beforeunload guard so users get a browser-native warning dialog when leaving a tab with unsaved changes. This integrates as a composable beforeunload event listener (via addEventListener / removeEventListener) rather than clobbering window.onbeforeunload.
Changes:
- Implement
prevent_unloadandallow_unloadactions using a shared module-levelbeforeunloadhandler. - Register both actions in Browser action registration.
- Add browser tests covering arming, disarming, and idempotent re-arming, plus README documentation entries.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/browser/before_unload.test.js | Adds tests validating the beforeunload guard can be armed/disarmed and remains removable after repeated arming. |
| src/actions/browser.ts | Implements and registers prevent_unload/allow_unload via a named beforeunload event handler. |
| README.md | Documents the two new Browser actions in the public action list. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
5532301 to
3f3ae84
Compare
Arm and disarm a `beforeunload` guard via a named `addEventListener` handler, so the browser warns before leaving with unsaved changes. Closes marcoroth#24.
Add the two new Browser Actions to the README action list.
3f3ae84 to
0b60436
Compare
marcoroth
left a comment
There was a problem hiding this comment.
What do you think about set_beforeunload and clear_beforeunload?
|
Hi @marcoroth
I considered those names as well. I guess that would make the underlying implementation clearer (and align better with other action naming). |
Implements two Browser actions to arm and disarm a
beforeunloadguard, so the server can warn the user before they leave a tab with unsaved changes.Approach
addEventListener("beforeunload", …)with a single module-level named handler, not thewindow.onbeforeunloadproperty. This composes with any otherbeforeunloadlisteners and removes only its own handler — assigning the property would clobber (and be clobbered by) other scripts.preventDefault()(the modern trigger) and setsreturnValuefor broader browser support. No browser supported by Turbo 7.2+ displays thereturnValuestring, so the generic confirmation dialog is shown.prevent_unloadis idempotent (re-adding the same handler reference is a no-op).onbeforeunloadstrings and show a generic dialog.Changes
prevent_unload/allow_unloadtosrc/actions/browser.tsand registered them.test/browser/before_unload.test.js— arm, disarm, and idempotency cases (assertsevent.defaultPrevented).Closes #24
🤖 Generated with Claude Code