我正在尝试使用runtime.connectNative和postMessage实现一个chrome扩展。我遵循铬文档,下载了我试图运行的本机消息传递示例,没有任何更改,而本地主机应用程序的代码可以找到这里。
但是,我得到了错误:未定义的TypeError:无法读取未定义的属性'connectNative‘。
此错误是从javascript扩展名文件中触发的,如下所示:
港口= chrome.runtime.connectNative(hostName);
当扩展从清单中加载时,如下所示:
"app": {
"launch": {
"local_path": "main.html"
}
}请问有什么办法解决这个问题吗?
Chrome版本34,在windows 7,8.1上进行测试
发布于 2014-08-07 23:35:53
当前的问题是,您没有正确地运行示例代码。更大的问题是Google没有提供关于如何使用这个示例代码的全面文档。
您引用的本机消息传递示例仅链接到Chrome扩展的示例代码。在搜索之后,我找到了本机消息传递主机应用程序的相关示例代码。要同时获得Chrome扩展和本地消息传递主机应用程序的示例代码,您需要下载nativeMessaging.zip。在这个zip文件中,您还可以找到一些关于如何在Windows、Linux和Mac上安装本机消息传递主机的简要说明。我现在将告诉您,这些说明是不完整的,因为它们没有告诉您如何安装Chrome扩展。此外,用于安装和卸载本机消息传递主机的脚本不能在OS上正常工作。请参阅下面的安装说明和更正的脚本。
如何安装示例扩展和本机主机应用程序
chrome://extensions/nativeMessaging目录,并选择要导入的app目录
chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.shnativeMessaging/host/install_host.sh和nativeMessaging/host/uninstall_host.sh中的一些bug。有关修改后的脚本,请参阅下面。nativeMessaging/README.txt中的说明
chrome://apps/
修正的
#!/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.修正的
#!/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.https://stackoverflow.com/questions/23385991
复制相似问题