Chrome扩展NativeMessaging'connectNative'未定义

use*_*674 4 javascript google-chrome-extension chrome-native-messaging

我正在尝试使用runtime.connectNative和postMessage实现chrome扩展.我正在关注chrome文档,下载了我尝试运行的本机消息传递示例,没有任何更改,而本机宿主应用程序的代码可以在这里找到.

但是,我收到错误:Uncaught TypeError:无法读取未定义的属性'connectNative'.

该错误是从javascript扩展文件触发的,在此行中:
port = chrome.runtime.connectNative(hostName);

正在从清单加载扩展名,如下所示:

"app": {
   "launch": {
      "local_path": "main.html"
   }
}
Run Code Online (Sandbox Code Playgroud)

有什么想法可以解决问题吗?

Chrome版本34,在Windows 7,8.1上测试过

Hai*_*Dog 7

当前的问题是您没有正确运行示例代码.更大的问题是Google没有提供有关如何使用此示例代码的全面文档.

您引用的Native Messaging示例仅链接到Chrome扩展程序的示例代码.在搜索之后,我能够找到本机消息传递主机应用程序的相关示例代码.要同时获取Chrome扩展程序和本机消息传递主机应用程序的示例代码,您需要下载nativeMessaging.zip.在该zip文件中,您还可以找到有关如何在Windows,Linux和Mac OS X上安装本机消息传递主机的简要说明.我现在会告诉您说明不完整,因为它们没有告诉您如何安装Chrome扩展程序.此外,用于安装和卸载本机消息传递主机的脚本在OS X上不能正常工作.请参阅下面的安装说明和更正的脚本.

如何安装示例扩展和本机主机应用程序

  1. 下载并解压缩nativeMessaging.zip文件.
  2. 安装Chrome扩展程序
    1. 在Chrome中输入chrome://extensions/地址栏
    2. 单击"加载解压缩的扩展名..."按钮
    3. 导航到解压缩的nativeMessaging目录并选择app要导入的目录
  3. 安装本机消息传递主机应用程序
    1. 对于OS X和Linux,您需要为某些文件添加执行权限.运行命令:chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.sh
    2. 对于OS X,你需要修复一些bug中nativeMessaging/host/install_host.shnativeMessaging/host/uninstall_host.sh.请参阅下面的更正脚本.
    3. 对于OS X,Linux和Windows,请按照说明进行操作 nativeMessaging/README.txt
  4. 运行Chrome扩展程序
    1. 在Chrome中输入chrome://apps/地址栏
    2. 单击Native Messaging Example应用程序图标
    3. 应用程序加载后,您应该看到一个名为"Connect"的按钮.单击该按钮,您应该会看到自动启动本机消息传递主机应用程序.

更正 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.
Run Code Online (Sandbox Code Playgroud)

更正 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.
Run Code Online (Sandbox Code Playgroud)