5

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)
                    }
                }
            })
        })
    })
}
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
pvieito
  • 117
  • 2
  • 8

3 Answers3

7

Yes there is a getActiveTab:

SFSafariApplication.getActiveWindow { (window) in
    window?.getActiveTab { (tab) in 
        tab?.getActivePage(completionHandler: { (page) in
        }
    }
}
Emmanuel Sellier
  • 526
  • 1
  • 5
  • 13
6

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.

breakingobstacles
  • 2,815
  • 27
  • 24
  • So there is nothing like [`activeTab` in WebExtension](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activeTab_permission)? – Franklin Yu Sep 19 '18 at 05:12
1

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.

amer
  • 1,528
  • 1
  • 14
  • 22
ooki2day
  • 11
  • 1