runtime.sendNativeMessage()
Sends a single message from an extension to a native application.
This takes two mandatory parameters: the name of the native application and a JSON object which is the message to send it. The browser will launch the native application and deliver the message.
This is an asynchronous function that returns a Promise
. The first message sent by the native application is treated as a response to the sendNativeMessage()
call, and the promise will be fulfilled with this message as a parameter. Note that you can't use runtime.onMessage
to get responses from the application: you must use the callback function instead.
A new instance of the application is launched for call to runtime.sendNativeMessage()
. The browser will terminate the native application after getting a reply. To terminate a native application, the browser will close the pipe, give the process a few seconds to exit gracefully, and then kill it if it has not exited.
For more information, see Native messaging.
Syntax
let sending = browser.runtime.sendNativeMessage(
application,
message
)
Parameters
-
application
-
string
. The name of the native application. This must match the "name" property in the native application's manifest file.
-
message
-
object
. A JSON object that will be sent to the native application.
Return value
A Promise
. If the sender sent a response, this will be fulfilled with the response as a JSON object. Otherwise it will be fulfilled with no arguments. If an error occurs while connecting to the native application, the promise will be rejected with an error message.
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
sendNativeMessage |
29 |
15 |
50 |
? |
16 |
14See the documentation on developer.apple.com about native messaging in Safari.
|
? |
? |
68 |
? |
15See the documentation on developer.apple.com about native messaging in Safari.
|
? |
Examples
Here's a background script that sends a "ping" message to the "ping_pong" app and logs the response, whenever the user clicks the browser action:
function onResponse(response) {
console.log(`Received ${response}`);
}
function onError(error) {
console.log(`Error: ${error}`);
}
browser.browserAction.onClicked.addListener(() => {
console.log("Sending: ping");
let sending = browser.runtime.sendNativeMessage("ping_pong", "ping");
sending.then(onResponse, onError);
});