如何在Safari App Extension中获得活动标签?

pvi*_*ito 3 safari macos swift safari-app-extension

当我按照Apple的示例进行操作时,总是得到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)
                    }
                }
            })
        })
    })
}
Run Code Online (Sandbox Code Playgroud)

bre*_*les 6

您的 Safari 应用程序扩展程序只能访问您的扩展程序配置为访问的站点的选项卡。

在您的 Info.plist 中,验证SFSafariWebsiteAccess字典

  • 具有等于“某些”或“全部”的级别

  • Allowed Domains数组中列出了所有必需的域(如果 Level=" Some ")。

请注意,您的扩展程序的网站访问权限显示在Safari > Preferences > Extensions 中。我们鼓励您将网站访问权限限制为“某些”(和明确的域),除非您确实需要访问每个网站。


Emm*_*ier 5

是的,有一个getActiveTab:

SFSafariApplication.getActiveWindow { (window) in
    window?.getActiveTab { (tab) in 
        tab?.getActivePage(completionHandler: { (page) in
        }
    }
}
Run Code Online (Sandbox Code Playgroud)