javascriptgoogle-chrome-extensionchrome-native-messaging

Chrome Extension NativeMessaging 'connectNative' undefined


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


Solution

  • 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

    1. Download and unzip the nativeMessaging.zip file.
    2. Install the Chrome extension
    3. In Chrome enter chrome://extensions/ in the address bar
    4. Click the “Load unpacked extension...” button
    5. Navigate to the unzipped nativeMessaging directory and select the app directory for import
    6. Install the native messaging host application
    7. For OS X and Linux you’ll need to add execute permission to some of the files. Run the command: chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.sh
    8. For OS X you’ll need to fix some bugs in nativeMessaging/host/install_host.sh and nativeMessaging/host/uninstall_host.sh. See below for the corrected scripts.
    9. For OS X, Linux and Windows follow the instructions in nativeMessaging/README.txt
    10. Run the Chrome extension
    11. In Chrome enter chrome://apps/ in the address bar
    12. Click on the Native Messaging Example app icon
    13. After the app loads you should see a single button named “Connect.” Click that button and you should see the native messaging host application launch automatically.

    Corrected nativeMessaging/host/install_host.sh

    #!/bin/sh
    # Copyright 2013 The Chromium Authors. All rights reserved.
    # Use of this source code is governed by a BSD-style license that can be
    # found in the LICENSE file.
    
    set -e
    
    DIR="$( cd "$( dirname "$0" )" && pwd )"
    if [ $(uname -s) == 'Darwin' ]; then
      if [ "$(whoami)" == "root" ]; then
        TARGET_DIR="/Library/Google/Chrome/NativeMessagingHosts"
      else
        TARGET_DIR="$HOME/Library/Application Support/Google/Chrome/NativeMessagingHosts"
      fi
    else
      if [ "$(whoami)" == "root" ]; then
        TARGET_DIR="/etc/opt/chrome/native-messaging-hosts"
      else
        TARGET_DIR="$HOME/.config/google-chrome/NativeMessagingHosts"
      fi
    fi
    
    HOST_NAME=com.google.chrome.example.echo
    
    # Create directory to store native messaging host.
    mkdir -p "$TARGET_DIR"
    
    # Copy native messaging host manifest.
    cp "$DIR/$HOST_NAME.json" "$TARGET_DIR"
    
    # Update host path in the manifest.
    HOST_PATH="$DIR/native-messaging-example-host"
    ESCAPED_HOST_PATH=${HOST_PATH////\\/}
    sed -i -e "s/HOST_PATH/$ESCAPED_HOST_PATH/" "$TARGET_DIR/$HOST_NAME.json"
    
    # Set permissions for the manifest so that all users can read it.
    chmod o+r "$TARGET_DIR/$HOST_NAME.json"
    
    echo Native messaging host $HOST_NAME has been installed.
    

    Corrected nativeMessaging/host/uninstall_host.sh

    #!/bin/sh
    # Copyright 2013 The Chromium Authors. All rights reserved.
    # Use of this source code is governed by a BSD-style license that can be
    # found in the LICENSE file.
    
    set -e
    
    if [ $(uname -s) == 'Darwin' ]; then
      if [ "$(whoami)" == "root" ]; then
        TARGET_DIR="/Library/Google/Chrome/NativeMessagingHosts"
      else
        TARGET_DIR="$HOME/Library/Application Support/Google/Chrome/NativeMessagingHosts"
      fi
    else
      if [ "$(whoami)" == "root" ]; then
        TARGET_DIR="/etc/opt/chrome/native-messaging-hosts"
      else
        TARGET_DIR="$HOME/.config/google-chrome/NativeMessagingHosts"
      fi
    fi
    
    HOST_NAME=com.google.chrome.example.echo
    rm "$TARGET_DIR/com.google.chrome.example.echo.json"
    echo Native messaging host $HOST_NAME has been uninstalled.