Finding out when a tab has finished loading

Hello,

In order to handle some deep linking functionality, our Safari App Extension needs to know when tabs containing pages that the extension has access to have loaded, so we can do appropriate redirects, close temporary tabs, etc. Until now we had been using the SFSafariExtensionHandler function page(_ page: SFSafariPage, willNavigateTo url: URL?) for this purpose, but realized that several cases weren't being handled properly.

Upon further inspection is appears that it's necessary to wait until the tabs in question have progressed to a certain point in the loading process, otherwise the tab returned by getContainingTab(completionHandler: @escaping (SFSafariTab) -> Void) may be incorrect, and in some cases logic may be unexpectedly run twice or more due to multiple tabs incorrectly matching url checking conditionals.

Moving the logic into a DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) block, along with using the URL provided by a nested page.getContainingTab, containingTab.getActivePage, and page?.getPropertiesWithCompletion instead of of the URL function parameter fixes these issues almost entirely, but it feels ugly and brittle.

Is there a better way to get handles for tabs once they're fully initialized? I've looked through the documentation several times and it seems like what's possible Swift-side is quite restricted.

Thanks!

Could you inject a script on the page that listens for DOMContentLoaded or the onload event and sends a message to the native code of the Safari App Extension?

If that doesn't work, then I'd recommend trying Safari Web Extensions. They have a more fleshed out API for page navigation and loading events.

Finding out when a tab has finished loading
 
 
Q