views:

24

answers:

1

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?

A: 

The problem is that all messages save one (canLoad) are asynchronous. IMO, the canLoad is a bit of a hack itself, but I've used it effectively when I desperately need synchronous messaging for some reason. I asked an analogous question a while back that may help illustrate the canLoad solution/hack.

Rob Wilkerson
Thanks. Yeah, abusing `canLoad` is pretty hacky, but there's not much else one can do.
anshuchimala