Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get active tab in a Safari App Extension?

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)
                    }
                }
            })
        })
    })
}
like image 468
pvieito Avatar asked Sep 27 '16 12:09

pvieito


People also ask

What is an active tab Safari?

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.

How do I add an extension to Safari app?

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.

Why is my extension not showing in Safari?

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.

What is Safari app extension?

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.


3 Answers

Yes there is a getActiveTab:

SFSafariApplication.getActiveWindow { (window) in
    window?.getActiveTab { (tab) in 
        tab?.getActivePage(completionHandler: { (page) in
        }
    }
}
like image 56
Emmanuel Sellier Avatar answered Nov 03 '22 00:11

Emmanuel Sellier


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.

like image 27
breakingobstacles Avatar answered Nov 03 '22 00:11

breakingobstacles


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.

like image 42
ooki2day Avatar answered Nov 03 '22 01:11

ooki2day