Chrome扩展程序NativeMessaging'connectNative'未定义 [英] Chrome Extension NativeMessaging 'connectNative' undefined
问题描述
我试图使用runtime.connectNative和postMessage实现一个Chrome扩展。我遵循 chrome文档,下载了本地消息传递示例,我试图在没有任何更改的情况下运行,而本地主机应用程序的代码可以在 here 。
但是,我收到错误:
Uncaught TypeError:无法读取未定义的属性'connectNative'。
错误是由javascript扩展触发的文件,在此行中:
port = chrome.runtime.connectNative(hostName);
当扩展程序正在从清单中加载时, :
app:{
launch:{
local_path:main。 html
}
任何想法如何解决这个问题吗?
Chrome版本34,在Windows 7,8.1上测试
直接的问题是您没有正确运行示例代码。更大的问题是Google没有提供关于如何使用这个示例代码的全面文档。
您引用的本地消息传递示例仅链接到Chrome扩展的示例代码。在四处搜索后,我能够找到本地消息传递主机应用程序的相关示例代码。要同时获得Chrome扩展和本地消息主机应用程序的示例代码,您需要下载 nativeMessaging.zip 。在该zip文件中,您还可以找到关于如何在Windows,Linux和Mac OS X上安装本地消息传递主机的一些简要说明。我现在告诉您,说明不完整,因为它们没有告诉您如何安装Chrome扩展程序。此外,用于安装和卸载本地消息传递主机的脚本在OS X上不起作用。有关我的安装说明和更正的脚本,请参阅下文。
$ b 安装示例扩展和本地主机应用程序
- 下载并解压缩nativeMessaging.zip文件。 $ b
- 安装Chrome扩展程序
- 在Chrome中,在地址栏中输入
chrome:// extensions /
/ li>
- 点击加载解压后的扩展...按钮
- 导航到解压后的
nativeMessaging
目录并选择应用程序
目录进行导入
- 在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 /
- 单击本地消息示例应用程序图标
- 在应用程序加载后,您应该看到一个名为Connect的按钮。单击该按钮,您应该会自动查看本地消息传递主机应用程序的启动。
- 在Chrome中输入
- Download and unzip the nativeMessaging.zip file.
- Install the Chrome extension
- In Chrome enter
chrome://extensions/
in the address bar - Click the "Load unpacked extension..." button
- Navigate to the unzipped
nativeMessaging
directory and select theapp
directory for import - Install the native messaging host application
- 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
- For OS X you’ll need to fix some bugs in
nativeMessaging/host/install_host.sh
andnativeMessaging/host/uninstall_host.sh
. See below for the corrected scripts. - For OS X, Linux and Windows follow the instructions in
nativeMessaging/README.txt
- For OS X and Linux you’ll need to add execute permission to some of the files. Run the command:
- Run the Chrome extension
- In Chrome enter
chrome://apps/
in the address bar - Click on the Native Messaging Example app icon
- 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.
- In Chrome enter
$ b
更正 nativeMessaging / host / install_host.sh
#!/ bin / sh
#版权所有2013 The Chromium Authors。版权所有。
#此源代码的使用受BSD样式许可证的约束,该许可证可以在LICENSE文件中找到
#。
set -e
DIR =$(cd$(dirname$ 0)&& pwd)
if [$(uname -s)=='Darwin'];然后
if [$(whoami)==root];然后
TARGET_DIR =/ Library / Google / Chrome / NativeMessagingHosts
TARGET_DIR =$ HOME / Library / Application Support / Google / Chrome / NativeMessagingHosts
fi
else
if [$(whoami)==root];然后
TARGET_DIR =/ etc / opt / chrome / native-messaging-hosts
其他
TARGET_DIR =$ HOME / .config / google-chrome / NativeMessagingHosts
fi
fi
HOST_NAME = com.google.chrome.example.echo
#创建存储本地邮件主机的目录。
mkdir -p$ TARGET_DIR
#复制本地消息传递主机清单。
cp$ DIR / $ HOST_NAME.json$ TARGET_DIR
#更新清单中的主机路径。
HOST_PATH =$ DIR / native-messaging-example-host
ESCAPED_HOST_PATH = $ {HOST_PATH //// \\ /}
sed -i -es / HOST_PATH / $ ESCAPED_HOST_PATH /$ TARGET_DIR / $ HOST_NAME.json
#为清单设置权限,以便所有用户都可以读取它。
chmod o + r$ TARGET_DIR / $ HOST_NAME.json
已安装本地消息传递主机$ HOST_NAME。
更正 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: while the extension is being loaded from the manifest like so: Any ideas how to solve the problem please? Chrome version 34, tested on windows 7, 8.1 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 nativeMessaging / host / uninstall_host.sh
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $#$ / $ $ $ $ $#版权所有2013 The Chromium Authors。版权所有。
#此源代码的使用受BSD样式许可证的约束,该许可证可以在LICENSE文件中找到
#。
set -e
if [$(uname -s)=='Darwin'];然后
if [$(whoami)==root];然后
TARGET_DIR =/ Library / Google / Chrome / NativeMessagingHosts
TARGET_DIR =$ HOME / Library / Application Support / Google / Chrome / NativeMessagingHosts
fi
else
if [$(whoami)==root];然后
TARGET_DIR =/ etc / opt / chrome / native-messaging-hosts
其他
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
回显本地消息传递主机$ HOST_NAME已被卸载。
port = chrome.runtime.connectNative(hostName);"app": {
"launch": {
"local_path": "main.html"
}
}
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.
这篇关于Chrome扩展程序NativeMessaging'connectNative'未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!