2

I have this extension where I want to open a new tab in the JavaScript part of the extension. Before migrating it to a Safari App Extension I could just do

window.open(url, "_blank");

But when I run this in Safari 12 as an App Extension it adds the current link to the Reading List (?!).

When I run the code above in the console it opens a new tab of the url, but I have to enable popups in the Safari Preferences. I can't find anything in Apple's very poor documentation.

Is this even possible on "client side", or do I have to handle this in the Swift code?

simeg
  • 1,889
  • 2
  • 26
  • 34

1 Answers1

5

If you'd like to open a new tab using Safari App Extensions, you'd need to do that in native code.

You can achieve this by obtaining the active window, and then calling the openTab() method on that window.

Below is a snippet that achieves this:

let myUrl = URL(string: "https://google.com")


// This grabs the active window.
SFSafariApplication.getActiveWindow { (activeWindow) in

        // Request a new tab on the active window, with the URL we want.
        activeWindow?.openTab(with: myUrl, makeActiveIfPossible: true, completionHandler: {_ in
            // Perform some action here after the page loads if you'd like.
        })
    }
  • I didn't know I had access to `SFSafariApplication` so I didn't even consider this.. this solves a lot of headache for me so big thanks! – simeg Jul 22 '18 at 23:22
  • Do you know if it's possible to define the position of the new tab? Ideally I'd like it next to the current tab because the page I'm opening is related to the current one, but currently the new tab appears at the far right at the end of all open tabs. – Matt Sephton Nov 24 '19 at 20:18