SafariActivateEvent in Safari App Extension

I want to perform an action only if some URL is currently active (in focus) in mutli tab/window scenario.


Ref: https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/SafariAppExtension_PG/HandlingCommonScenarios.html#//apple_ref/doc/uid/TP40017319-CH12-SW1


As per above link

"A Safari app extension does not have a global HTML page. Instead, any work that you used to do in the global HTML page must now be done in the native app extension."


In absense of global page, how do I listen for events like SafariActivateEvent?

I tried focus event. It is however triggered only if an input element is focused.

Replies

Does your extension have a toolbar item?


If it does, you can implement:


/// This is called when Safari's state changed in some way that would require the extension's toolbar item to be validated again.

- (void)validateToolbarItemInWindow:(SFSafariWindow *)window validationHandler:(void (^)(BOOL enabled, NSString *badgeText))validationHandler;


That will be called when the active tab/window changes, and you should be able to set up your state there.

Thanks bweinstein.

I have tried the tool bar option. Although it provides consistent notifications, the tool bar icon itself does not provide any useful function to the user. I can probably show a message on click explaining why the icon is required. Otherwise user may remove it.

It would have been better to have a notification to the extension app.

What would you like the activate event for? If your extension has any content scripts, you could also listen for web page visibility events inside of the content scripts.


https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

Thanks bweinstein.


The visibility change event, if I remember correctly, is only called if complete page is hidden and then made visible. e.g if I switch between two Safari windows (may be non-overlapping) using "command + `" shortcut then the event is not generated.


I tried a combination of different events.

For inconsistent focus events I have filed a rdar 35218644.