30

Native Messaging - Microsoft Edge Development | Microsoft Docs

 3 years ago
source link: https://docs.microsoft.com/en-us/microsoft-edge/extensions-chromium/developer-guide/native-messaging?tabs=linux
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Native messaging

  • 10/06/2020
  • 5 minutes to read

In this article

Extensions communicate with a native Win32 application installed on a user's device using message passing APIs. The native application host sends and receives messages with extensions using standard input and standard output. Extensions using native messaging are installed in Microsoft Edge similar to any other extension. However, native applications are not installed or managed by Microsoft Edge.

To acquire the extension and native application host, you have two distribution models.

  • Package your extension and the host together. When a user installs the package, both the extension and the host are installed.
  • Install your extension using the Microsoft Edge Add-ons store, and your extension prompts users to install the host.

To create your extension to send and receive messages with native application hosts, refer to the following steps.

Step 1 - Add permissions to the extension manifest

Add the nativeMessaging permission to the manifest.json file of the extension. The following code snippet is an example of manifest.json.

    {
          "name": "Native Messaging Example",
          "version": "1.0",
          "manifest_version": 2, 
          "description": "Send a message to a native application.",
          "app": { 
              "launch": { 
                  "local_path": "main.html" } 
                 }, 
          "icons": { 
              "128": "icon-128.png"}, 
          "permissions": ["nativeMessaging"] 
    }

Step 2 - Create your native messaging host manifest file

Native applications must provide a native messaging host manifest file. The manifest file contains the path to the native messaging host runtime, the method of communication with the extension, and a list of allowed extensions to which it communicates. The browser reads and validates the native messaging host manifest. The browser does not install or manage the native messaging host manifest file.

    {
        "name": "com.my_company.my_application",
        "description": "My Application",
        "path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe",
        "type": "stdio",
        "allowed_origins": [
            "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
        ]
    }

The host manifest file must be a valid JSON file that contains the following keys.

name

Specifies the name of the native messaging host. Clients pass this string to runtime.connectNative or runtime.sendNativeMessage.

  • This value must only contain lowercase alphanumeric characters, underscores, and dots.
  • This value must not start or end with a dot, and a dot must not be followed by another dot.

description

Describes the application.

path

Specifies the path to the native messaging host binary.

  • On Windows devices, you may use relative paths to the directory that contains the manifest file.
  • On macOS and Linux, the path must be absolute.

The host process starts with the current directory set to the directory that contains the host binary. For example (Windows), if this parameter is set to C:\Application\nm_host.exe, the binary is started using the current directory (C:\Application\).

type

Specifies the type of the interface used to communicate with the native messaging host. This value instructs Microsoft Edge to use stdin and stdout to communicate with the host.
The only acceptable value is stdio.

allowed_origins

Specifies the list of extensions that have access to the native messaging host. To enable your application to identify and communicate with an extension, in your native messaging host manifest file set the following value.

"allowed_origins": ["chrome-extension://{microsoft_catalog_extension_id}"]

Sideload your extension to test native messaging with the host.
To sideload your extension during development and retrieve microsoft_catalog_extension_id, complete the following steps.

  1. Navigate to edge://extensions, and then turn on the Developer mode toggle button.
  2. Choose Load unpacked, and then select your extension package to sideload.
  3. Choose OK.
  4. Navigate to edge://extensions page and verify your extension is listed.
  5. Copy the key from microsoft_catalog_extension_id (ID) from the extension listing on the page.

When you are ready to distribute your extension to users, publish your extension to the Microsoft Edge add-ons store. The extension ID of the published extension may differ from the ID used while sideloading your extension. If the ID changed, update allowed_origins in the host manifest file with the ID of your published extension.

Step 3 - Copy the native messaging host manifest file to your system

The final step involves copying the native messaging host manifest file to your computer, and ensuring it is configured correctly. To ensure your manifest file is placed in the expected location, complete the following the steps. The location varies by platform.

To store the manifest file, complete one of the following actions.

  • System-wide native messaging hosts, which are available to all users, are stored in a fixed location. The manifest file must be stored in following location.

    /etc/opt/edge/native-messaging-hosts
    
  • User-specific native messaging hosts, which are available to the current user only, are located in the NativeMessagingHosts subdirectory in the user profile directory. The manifest file must be stored in following location.

    ~/.config/microsoft-edge/NativeMessagingHosts
    

Ensure that you provide read permissions on the manifest file, and run permissions on the host runtime.

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons Attribution 4.0 International License.
The original page is found here.

This work is licensed under a Creative Commons Attribution 4.0 International License.

Is this page helpful?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK