You could have a variable saving that is set to true on focus and false on blur and then wait for that to be false before navigating. That wouldn’t stop browser refreshes and navigation tho
Here’s something similar Confirmation dialog on closing page