Chrome NativeMessaging extension 'connectNative' undefined

I am trying to implement a chrome extension using runtime.connectNative and postMessage. I follow the chrome documentation , downloaded an example of native messages that I am trying to run without any changes, and the code for the main host application can be found here .

However, I get an error: Uncaught TypeError: Unable to read the "connectNative" property from undefined.

The error is launched from the javascript extension file, in this line:
port = chrome.runtime.connectNative (hostName);

while the extension is loaded from the manifest as follows:

"app": {
   "launch": {
      "local_path": "main.html"
   }
}

Any ideas how to solve the problem, please?

Chrome 34, 7, 8.1

+4
1

, . , Google , .

Native Messaging Chrome. . Chrome, , nativeMessaging.zip. zip , Windows, Linux Mac OS X. , , , Chrome. , OS X. . .

-

  • nativeMessaging.zip.
  • Chrome
    • Chrome chrome://extensions/
    • " ..."
    • nativeMessaging app
    • OS X Linux . : chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.sh
    • OS X nativeMessaging/host/install_host.sh nativeMessaging/host/uninstall_host.sh. .
    • X, Linux Windows nativeMessaging/README.txt
  • Chrome
    • Chrome chrome://apps/
    • Native Messaging Example
    • "". , .

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.

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.
+5

Source: https://habr.com/ru/post/1538711/


All Articles