I'm messing around with Safari extensions, and I'd like my extension to respond to the user opening or closing tabs.
I have the extension injecting a script that runs when the new tab opens and notifies the global page that there's a new tab open. I want the script to also notify the global page when the tab closes. To do this, I'm having the injected script set the window.onbeforeunload
function to call safari.self.tab.dispatchMessage
. This works, but the problem is the tab closes and the Javascript gets killed before the message goes through. If, for example, I put an alert
after the dispatchMessage
to prevent the tab from closing until the user clicks OK, the message goes through fine, but this obviously isn't a very good user experience.
Is there a way to buy a little extra time right before the tab closes, or is there a better function to override for this or something?