When I follow the Apple example I always get a nil
activeTab
:
override func toolbarItemClicked(in window: SFSafariWindow) {
// This method will be called when your toolbar item is clicked.
window.getActiveTab(completionHandler: { (activeTab) in
print(activeTab)
activeTab?.getActivePage(completionHandler: { (activePage) in
print(activePage)
activePage?.getPropertiesWithCompletionHandler( { (properties) in
print(properties)
if properties?.url != nil {
let urlString = properties!.url.absoluteString
print("URL!", urlString)
let videoURL = self.youtubeDl(urlString: urlString)
if videoURL != nil {
window.openTab(with: videoURL!, makeActiveIfPossible: true, completionHandler: nil)
}
}
})
})
})
}
Dubbed as ActiveTab, the new Safari extension makes it easier for users to differentiate between active and inactive tabs in Safari. The tool essentially puts a line (of any desired color) underneath an active tab to enable users to easily spot the tab that they are currently using.
To create a Safari app extension: Launch Xcode and either open an existing project containing a macOS app or create a new one. Choose File > New > Target. From the list of templates in the Application Extension section, select Safari Extension, and click Next.
Open Safari and choose Safari > Preferences. Select the Extensions tab, find your extension, and select its checkbox. If you're developing your extension and it isn't visible in Safari Preferences, you need to allow unsigned extensions in Safari.
Overview. A Safari app extension can add new functionality to Safari by reading and modifying webpage content. These capabilities enhance the tools you use, the tasks you can accomplish, and the data you can access in your browser. A Safari app extension is uniquely useful because it can communicate with a native app.
Yes there is a getActiveTab:
SFSafariApplication.getActiveWindow { (window) in
window?.getActiveTab { (tab) in
tab?.getActivePage(completionHandler: { (page) in
}
}
}
Your Safari App Extension can only access the tabs of sites your extension is configured to access.
In your Info.plist, verify that the SFSafariWebsiteAccess dictionary:
Has a Level equal to "Some" or "All".
Has all required domains listed in the Allowed Domains array (if Level="Some").
Be aware that your extension's website access permissions are displayed in Safari > Preferences > Extensions. You are encouraged to limit your website access to "Some" (and explicit domains) unless you really do require access to every website.
You should go to info.plist, NSExtension->SFSafariWebsiteAccess
and set something ( for example * - for all sites) to Allowed domains. Without this option, I always got nil from the window. I think your extension doesn't work if the site is not allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With