The Chrome browser extension can also send and receive messages between client local apps by registering the local app of the browser's client as the "local messaging host (native messaging host") of the Chrome extension.
After the client's local app is registered as a "local messaging host" for the Chrome browser extension, the Chrome browser launches the local app in a standalone process and communicates the message through the standard input/output stream (stdin/stdout).
1) contents of the locally applied configuration file
Local app to be able to be a "local message host", there must be a Manifest.json configuration file (file name arbitrary), which is a valid JSON file, as shown below.
1 {2 "name": "Com.my_company.my_application",3 "description": "My Application ",4 " path ":" C:\\Program Files\\my Application\\chrome_native_messaging_host.exe ",5 " type ":" Stdio ",6 " Allowed_origins ": [ 7 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"8 ]9 }
The meaning and usage of the attributes are as follows.
Property name |
Meaning |
Name |
The identity name. Can only be composed of lowercase letters, numbers, _ and. Continuous presence |
Description |
Describe |
Path |
The full path to the local app, Linux and OS X must be absolute, and Windows can be a relative path based on the location of the configuration file |
Type |
The type of interface that communicates with the local messaging host. Currently only supports stdio, which means that the Chrome browser communicates with the local messaging host via stdin and stdout |
Allowed_origins |
A Chrome browser extension that authorizes communication with the local messaging host. Wildcard characters cannot be used |
2) path to the locally applied configuration file
The specific location of the configuration file is related to the operating system. On the Windows operating system, the configuration file can be located in any path, just modify the registry to indicate its location when the local app is installed.
Create key:
HKEY_LOCAL_MACHINE
\SOFTWARE\Google\Chrome\NativeMessagingHosts\
Com.my_company.my_application
Or
HKEY_CURRENT_USER
\SOFTWARE\Google\Chrome\NativeMessagingHosts\
Com.my_company.my_application
Set default defaults to the absolute path where the configuration file is located:
C:\path\to\nmh-manifest.json
You can also create a registry file directly. reg is as follows, which can be executed at installation:
1 Windows Registry Editor Version 5.002[hkey_current_user\software\google\chrome\ Nativemessaginghosts\com.my_company.my_application]3 @= "C:\\path\\to\\nmh-manifest.json"
For Linux and OS X operating systems, the location of the locally applied profile is first associated with the Chrome browser (Google Chrome or chromium) installed. Second, if it is a system-level local application, its configuration file should be in a fixed location. If it is a user-level local app, its configuration file is located in a subdirectory named Nativemessaginghosts under the user's home directory. The exact location is shown in the following table.
|
Google Chrome |
Chromium |
Linux System applications |
/etc/opt/chrome/native-messaging-hosts/com.my_company.my_application. JSON |
/etc/chromium/native-messaging-hosts/com.my_company.my_application. JSON |
Linux User applications |
~/.config/google-chrome/nativemessaginghosts/com.my_company.my_application. JSON |
~/.config/chromium/nativemessaginghosts/com.my_company.my_application. JSON |
OS x System applications |
/library/google/chrome/nativemessaginghosts/com.my_company.my_application. JSON |
/Library/Application support/chromium/nativemessaginghosts/com.my_company.my_application. JSON |
OS x User Apps |
~/library/application support/google/chrome/nativemessaginghosts/com.my_company.my_application. JSON |
~/library/application support/chromium/nativemessaginghosts/com.my_company.my_application. JSON |
3) message communication between chrome extensions and on-premises apps
To communicate with a client-side local app, the Chrome browser extension must first declare the permissions in the Chrome extension's Manifest.json configuration file as follows:
1 {2 "Permissions": [3 "nativemessaging"4 ], 5 }
The message communication between the Chrome extension and the client on-premises app is very similar to the communication between different chrome extensions, such as the following.
Create a port in the Chrome extension to listen to multiple messages from your local app:
1 varPort = chrome.runtime.connectNative (' com.my_company.my_application ');2 //parameter is the identity name that the local app declares in its configuration file3 4Port.onMessage.addListener (function(msg) {//processing functions After receiving a message5Console.log ("Received" +msg);6 });7Port.onDisconnect.addListener (function() {8Console.log ("Disconnected");9 });Ten OnePort.postmessage ({text: "Hello, My_application"});//send a message
You can also send a one-time message as follows in the Chrome browser extension without opening the port:
1 chrome.runtime.sendNativeMessage (2 ' com.my_company.my_application ',3 {text: "Hello" },4 function (response) {// receive return message after processing function 5 console.log ("Received" + response); 6 });
If the Chrome extension has an exception when calling the local app, the error message will be output in stderr. If an error occurs due to a violation of the protocol constraint on the local message, an error message will be output in the Chrome browser error log file. On the Linux and OS X operating systems, you can see the error message in the Command Line window by launching the Chrome browser from the command line. On the Windows operating system, When you start your Chrome browser with the--enable-logging parameter (in Chrome.exe's right-click Properties menu, the text box at the top of the general panel, followed by the parameters in the Chrome.exe), you can see the error message in the Chrome browser's log file.
4) Protocol constraints for local messages
After the client's local app is registered as a "local messaging host" for the Chrome browser extension, the Chrome browser launches the local app in a standalone process and communicates the message through the standard input/output stream (stdin/stdout).
The two-way communication between the chrome extension and the client's on-premises application takes the message mechanism, which is prefixed by the message length in JSON format, UTF-8 encoding, with 32 bits (operating system local byte order). The maximum size of a message sent from a local app to a Chrome browser extension is 1M bytes. The maximum size is 4G bytes for messages sent to the local app from the Chrome browser extension.
Chrome browser Extension Development Series 14: Local messaging mechanism native messaging