user1414202
user1414202

Reputation: 436

Chrome Native Messaging Chrome Extension

I am trying to incorporate native messaging with my chrome extension and my java application.

In my chrome extension's javascript, I have the following code. (I do have the nativeMessaging permission in my manifest.json)

function connectToNativeApp()
{
  console.log('connecting to native app...');
  port = chrome.runtime.connectNative('com.app.native');
  port.onMessage.addListener(onNativeMessage);
  port.onDisconnect.addListener(onDisconnected);
  console.log('connected to native app...');
}

When I send a message to the native app, my java app is supposed to be send backa message to the extension, but it is not. To test if it is actually connecting to the extension, I typed the host name incorrectly in the connectNative method but no error!?

I heard something about needing to add a registry key, as I am on windows, but I do not know how to and there is no NativeMessagingHosts folder under the chrome folder in regedit.

My native app folder is also in the same folder as the chrome extension. I tried looking for many tutorials, but found none useful for my problem. Why does Windows make everything complicated? :)

Please help. Thanks for your time and help. Subby

Upvotes: 0

Views: 2371

Answers (2)

omerfarukdogan
omerfarukdogan

Reputation: 869

The other answer has an issue. I've spent hours because of it.

In your com.app.native.messaging.json file, the last line must be like this:

"chrome-extension://<YOUR EXTENSION HASH>/"

I hadn't put the slashes, so the extension didn't give any errors but the application didn't start. Hope it helps.

Upvotes: 0

Sri
Sri

Reputation: 233

Import the following in the registry

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts]

[HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.app.native]
@="<YOURPATHHERE>\\com.app.native.messaging.json"

An example path could be "C:\\MyExtensions\\com.app.native.messaging.json" Make sure you have a json file named com.app.native.messaging.json (or any other name you fancy) in the folder. In the JSON file, add the following

{
  "name": "com.app.native.messaging",
  "description": "Your desctiption",
  "path": "<YOUR NATIVE APP NAME>.exe",
  "type": "stdio",
  "allowed_origins": [
  "chrome-extension: <YOUR EXTENSION HASH>"
  ]
}

As long as you ensure your native application is in the same folder as the JSON, you should be fine. In my case, I use a C++ native app and hence the exe extension. If you look at chrome sample, they've used a python script for the native app.

Remember that you will be listening on STDIN for incoming messages and send messages back to chrome using STDOUT. In both cases the messages are JSON formatted.

Upvotes: 1

Related Questions