How to send message from web extension’s background.js to containing APP

I’m developing safari web extension on macOS using Xcode. I need to send message from web extension’s background.js to containing APP.

As “Messaging a Web Extension’s Native App” describe, I create a port in background.js as follow:

let port = browser.runtime.connectNative("application.id");

In background.js send message as follow:

port.postMessage("Hello from JavaScript Port");

how can I receive this message in containing APP? I would be very grateful if you could give an example. Thanks!

Another problem is how can connectNative start the containg APP on macOS.

As runtime.connectNative describe in MDN(https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/connectNative)

“It starts the native application and returns a runtime.Port object to the caller.The caller can then use the Port to exchange messages with the native application using Port.postMessage() and port.onMessage”

In safari, connectNative seems cannot start the containing APP. How can I make it start the containing APP?

Replies

See https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html for some details about how to communicate/share information between your web extension's native code and the containing app.