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

BrainzPlayer: Warn before closing page if BrainzPlayer is playing #1907

Merged
merged 1 commit into from
Mar 17, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default class BrainzPlayer extends React.Component<
componentDidMount = () => {
window.addEventListener("storage", this.onLocalStorageEvent);
window.addEventListener("message", this.receiveBrainzPlayerMessage);

window.addEventListener("beforeunload", this.alertBeforeClosingPage);
// Remove SpotifyPlayer if the user doesn't have the relevant permissions to use it
const { spotifyAuth } = this.context;
if (
Expand All @@ -194,9 +194,23 @@ export default class BrainzPlayer extends React.Component<
componentWillUnMount = () => {
window.removeEventListener("storage", this.onLocalStorageEvent);
window.removeEventListener("message", this.receiveBrainzPlayerMessage);
window.removeEventListener("beforeunload", this.alertBeforeClosingPage);
this.stopPlayerStateTimer();
};

alertBeforeClosingPage = (event: BeforeUnloadEvent) => {
const { playerPaused } = this.state;
if (!playerPaused) {
// Some old browsers may allow to set a custom message, but this is deprecated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the current FF already doesn't show custom message when I tried the PR :/

event.preventDefault();
// eslint-disable-next-line no-param-reassign
event.returnValue = `You are currently playing music from this page.
Are you sure you want to close it? Playback will be stopped.`;
return event.returnValue;
}
return null;
};

receiveBrainzPlayerMessage = (event: MessageEvent) => {
if (event.origin !== window.location.origin) {
// Received postMessage from different origin, ignoring it
Expand Down