1

I have loaded an html file in a webkit view inside my app. The html file acts like a search engine and has the following code which gets triggered when the users hits the search action:

function search() {
    var input = document.getElementById("search_form_input_homepage").value;
    window.open("https://duckduckgo.com/?q=" + input);
}

However, I need the result url to open in Safari. As of now, window.open loads up the url in the default browser. How do I achieve this?

I found this link that talks about different url schemes in Safari. But I am very new to JavaScript. This is in fact the first time I am working with JS. So I could not understand how to implement this. Can anybody help me with this, please?

onetrueshady
  • 175
  • 1
  • 10
  • 3
    You have no control over this. Why would you want to enforce a specific browser? – Mitya Nov 30 '20 at 11:36
  • 2
    Why Safari specifically? – evolutionxbox Nov 30 '20 at 11:36
  • 1
    @Mitya Because it's a part of a safari extension. Ideally, I would do it in native code. But that is not possible in this current scenario as it has dependencies with the html file So I would like to do it using JS. – onetrueshady Nov 30 '20 at 11:43
  • 1
    @evolutionxbox Because it's a part of a safari extension. Ideally, I would do it in native code. But that is not possible in this current scenario as it has dependencies with the html file So I would like to do it using JS. – onetrueshady Nov 30 '20 at 11:44
  • 1
    Use a different protocol than `https`, which the browser will always open itself, and register safari as a protocol handler for that custom protocol with the operating system. – Bergi Nov 30 '20 at 11:44
  • 1
    @Bergi how? I am very inexperienced in JS. – onetrueshady Nov 30 '20 at 11:46
  • 1
    @onetrueshady Research "custom protocol handler" and/or study the extension API of Safari – Bergi Nov 30 '20 at 11:52

2 Answers2

1

I managed to do this using Swift. I added the following webkit delegate method.

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        NSWorkspace.shared.open([navigationAction.request.url!], withAppBundleIdentifier: "com.apple.safari", options: .default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
    return nil
}
onetrueshady
  • 175
  • 1
  • 10
0

You can't control which browser to open via JavaScript code, it's related to the operating system / user default selection.

Or Assayag
  • 5,662
  • 13
  • 57
  • 93