我正在尝试使用 runtime.connectNative 和 postMessage 实现 chrome 扩展。我正在关注 chrome documentation , 下载了 native messaging example我试图在没有任何更改的情况下运行,而可以找到 native 主机应用程序的代码 here .
但是,我收到错误: 未捕获的类型错误:无法读取未定义的属性“connectNative”。
错误是由 javascript 扩展文件触发的,在这一行中:
port = chrome.runtime.connectNative(主机名);
像这样从 list 中加载扩展时:
"app": {
"launch": {
"local_path": "main.html"
}
}
请问有什么解决办法吗?
Chrome 版本 34,在 Windows 7、8.1 上测试
最佳答案
直接的问题是您没有正确运行示例代码。更大的问题是谷歌没有提供关于如何使用这个示例代码的全面文档。
您引用的 native 消息示例仅链接到 Chrome 扩展程序的示例代码。在四处搜索之后,我找到了本地消息传递主机应用程序的相关示例代码。要同时获取 Chrome 扩展程序和 native 消息传递主机应用程序的示例代码,您需要下载 nativeMessaging.zip .在该 zip 文件中,您还会找到一些关于如何在 Windows、Linux 和 Mac OS X 上安装 native 消息传递主机的简要说明。我现在要告诉您,这些说明不完整,因为它们没有告诉您如何安装Chrome 扩展程序。此外,用于安装和卸载 native 消息传递主机的脚本在 OS X 上无法正常工作。请参阅下面的安装说明和更正后的脚本。
如何安装示例扩展和 native 主机应用程序
- 下载并解压缩 nativeMessaging.zip 文件。
- 安装 Chrome 扩展程序
- 在 Chrome 的地址栏中输入
chrome://extensions/
- 点击“加载解压后的扩展...”按钮
- 导航到解压缩的
nativeMessaging
目录并选择要导入的app
目录
- 在 Chrome 的地址栏中输入
- 安装本地消息传递主机应用程序
- 对于 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
中的一些错误。请参阅下面的更正脚本。 - 对于 OS X、Linux 和 Windows,请遵循
nativeMessaging/README.txt
中的说明
- 对于 OS X 和 Linux,您需要为某些文件添加执行权限。运行命令:
- 运行 Chrome 扩展程序
- 在 Chrome 的地址栏中输入
chrome://apps/
- 点击 Native Messaging Example 应用图标
- 应用加载后,您应该会看到一个名为“连接”的按钮。单击该按钮,您应该会看到 native 消息传递主机应用程序自动启动。
- 在 Chrome 的地址栏中输入
更正了 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.
关于javascript - Chrome 扩展 NativeMessaging 'connectNative' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23385991/