Safari Web Extension Fails to Send Native Message

631 Views Asked by At

I'm trying to create a Safari Web Extension to my native app. I want to have a popup with a button which when clicked will communicate with my native app:

    browser.runtime.sendNativeMessage({message: "Open Main App"}, function(response) {
    console.log("Received sendNativeMessage response:");
    console.log(response);
    });

However, the app fails to receive the message sent from the Safari Web Extension:

    func beginRequest(with context: NSExtensionContext) {

    its the function that handles all things
    then
    
    if msg.contains("Open Main App") == true {
      print("NEED TO OPEN")
      guard let url = URL(string: "player:Vacation?index=1") else {
         return
      }
      NSWorkspace.shared.open(url)
    }

Safari log showed this error message:

  ReferenceError: Can't find variable: browser

What is wrong?

1

There are 1 best solutions below

1
On

Did you use browser.runtime.sendNativeMessage from the background script? You also need to set the permission in the manifest file. Like so: "permissions": [ "nativeMessaging" ]