Modules

runtime.sendNativeMessage()

Cette traduction est incomplète. Aidez à traduire cet article depuis l'anglais.

Envoie un seul message d'une extension à une application native.

Cela prend deux paramètres obligatoires : le nom de l'application native et un objet JSON qui est le message à envoyer. Le navigateur lancera l'application native et délivrera le message.

C'est une fonction asynchrone qui renvoie une 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

var sending = browser.runtime.sendNativeMessage(
  application,             // string
  message                  // object
)

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

ChromeEdgeFirefoxFirefox for AndroidOpera
Support simple291550 Non16

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}`);
}

/*
On a click on the browser action, send the app a message.
*/
browser.browserAction.onClicked.addListener(() => {
  console.log("Sending:  ping");
  var sending = browser.runtime.sendNativeMessage("ping_pong", "ping");
  sending.then(onResponse, onError);
});

Remerciements :

Cette API est basée sur l'API Chromium chrome.runtime. Cette documentation est dérivée de runtime.json dans le code de Chromium code.

Les données de compatibilité relatives à Microsoft Edge sont fournies par Microsoft Corporation et incluses ici sous la licence Creative Commons Attribution 3.0 pour les États-Unis.

Étiquettes et contributeurs liés au document

 Contributeurs à cette page : hellosct1
 Dernière mise à jour par : hellosct1,