I am trying to implement a chrome extension using runtime.connectNative and postMessage. I am following the chrome documentation, downloaded the native messaging example which I'm trying to run without any changes, while the code for the native host application can be found here.
However, I'm getting the error: Uncaught TypeError: Cannot read property 'connectNative' of undefined.
The error is being triggered from the javascript extension file, in this line:
port = chrome.runtime.connectNative(hostName);
while the extension is being loaded from the manifest like so:
"app": {
"launch": {
"local_path": "main.html"
}
}
Any ideas how to solve the problem please?
Chrome version 34, tested on windows 7, 8.1
The immediate problem is that you are not running the sample code correctly. The larger problem is that Google has not provided comprehensive documentation on how to use this sample code.
The Native Messaging example you referenced only links to the sample code for the Chrome extension. After searching around I was able to find related sample code for the native messaging host application. To get the sample code for both the Chrome extension and native messaging host application together you'll want to download nativeMessaging.zip. In that zip file you'll also find some brief instructions on how to install the native messaging host on Windows, Linux and Mac OS X. I'll tell you right now that the instructions are incomplete as they do not tell you how to install the Chrome extension. Additionally the scripts for installing and uninstalling the native messaging host do not work as-is on OS X. See below for my installation instructions and corrected scripts.
How to install the sample extension and native host application
chrome://extensions/
in the address barnativeMessaging
directory and select theapp
directory for importchmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.sh
nativeMessaging/host/install_host.sh
andnativeMessaging/host/uninstall_host.sh
. See below for the corrected scripts.nativeMessaging/README.txt
chrome://apps/
in the address barCorrected
nativeMessaging/host/install_host.sh
Corrected
nativeMessaging/host/uninstall_host.sh