fiatjaf / nos2x

nostr signer extension
281 stars 56 forks source link

Handle "window:beforeunload" and send dialog reject result #6

Closed sondreb closed 2 years ago

sondreb commented 2 years ago

When the user clicks the X to dismiss the popup window, the caller (app) does not receive any rejection. This can be handled by implementing an handler for the beforeunload event on the DOM.

sondreb commented 2 years ago

With the single popup mutex, the lock is never released if the user clicks X as oppose to "Cancel". To fix it, user must restart browser or enable/disable the extension.

While working on handling the beforeunload event, it appears it's not entirely deterministic and depends on user interaction with the window. I have not been able to find any specific patterns, even if I click within the window, sometimes the beforeunload is simply not called. If I open the debug console on popup with an debugger statement, then the unload event it always called.

MDN: As of Chrome 60, the confirmation [will be skipped](https://chromestatus.com/feature/5082396709879808) if the user has not performed a gesture in the frame or page since it was loaded. Pressing F5 in the page seems to count as user interaction, whereas mouse-clicking the refresh arrow or pressing F5 with Chrome DevTools focused does not count as user interaction (as of Chrome 81).

A better solution will be to actually listen to the closing (finally) on windows.create (returns a Promise).

I'll get back to this issue when I have a good solution, working on it now.

sondreb commented 2 years ago

Got a working solution here, it will listen to the close event and perform a call to handlePromptMessage with condition 'no' if the prompt object is still there after window close:

nos2x-exit

Previously if users dismissed with X, the extension wouldn't be able to popup new windows.

PR incoming!

fiatjaf commented 2 years ago

Nice animation.