Javascript Chrome Extension NativeMessaging';connectNative';未定义
我正在尝试使用runtime.connectNative和postMessage实现chrome扩展。我正在遵循下载的,我正在尝试在不做任何更改的情况下运行,同时可以找到本机主机应用程序的代码 但是,我得到了一个错误: 未捕获的TypeError:无法读取未定义的属性“connectNative” 该错误是从javascript扩展文件触发的,在这一行:Javascript Chrome Extension NativeMessaging';connectNative';未定义,javascript,google-chrome-extension,chrome-native-messaging,Javascript,Google Chrome Extension,Chrome Native Messaging,我正在尝试使用runtime.connectNative和postMessage实现chrome扩展。我正在遵循下载的,我正在尝试在不做任何更改的情况下运行,同时可以找到本机主机应用程序的代码 但是,我得到了一个错误: 未捕获的TypeError:无法读取未定义的属性“connectNative” 该错误是从javascript扩展文件触发的,在这一行: port=chrome.runtime.connectNative(主机名) 从清单加载扩展时,如下所示: "app": { "laun
port=chrome.runtime.connectNative(主机名) 从清单加载扩展时,如下所示:
"app": {
"launch": {
"local_path": "main.html"
}
}
有什么办法解决这个问题吗
Chrome版本34,在windows 7、8.1上测试,直接的问题是您没有正确运行示例代码。更大的问题是谷歌没有提供关于如何使用这个示例代码的全面文档 您引用的本机消息传递示例仅链接到Chrome扩展的示例代码。在四处搜索之后,我找到了本机消息传递主机应用程序的相关示例代码。要同时获得Chrome扩展和本机消息传递主机应用程序的示例代码,您需要下载。在这个zip文件中,您还可以找到一些关于如何在Windows、Linux和Mac OS X上安装本机消息主机的简要说明。我现在就告诉您,这些说明不完整,因为它们没有告诉您如何安装Chrome扩展。此外,用于安装和卸载本机消息主机的脚本与OS X上的脚本不一样。有关我的安装说明和更正的脚本,请参阅下文 如何安装示例扩展和本机主机应用程序
chrome://extensions/地址栏中的
nativeMessaging
目录,并选择要导入的app
目录chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native messaging示例host nativeMessaging/host/uninstall_host.sh
nativeMessaging/host/install_host.sh
和nativeMessaging/host/uninstall_host.sh
中的一些错误。有关更正的脚本,请参见下文nativeMessaging/README.txt
chrome://apps/地址栏中的
nativeMessaging/host/install\u 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.
#!/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.
更正了nativeMessaging/host/uninstall\u 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.
#!/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.
我想提供一个python 3版本的脚本来替换本机消息传递示例主机。它是用ChromeV86测试的,工作正常。请注意,tkinter窗口关闭时python内核崩溃-这是因为线程内部的二进制数据交换导致线程被硬锁定(更多信息)。我添加了一个从chrome应用程序发送的退出命令,以停止线程等待另一个stdin。收到它之后,python不会在退出时崩溃 Python 3版本(使用3.7.4测试):
#一个简单的本机消息传递主机。显示包含传入消息的Tkinter对话框
#这也允许将消息发送回webapp。
导入结构
导入系统
导入线程
将队列作为队列导入
尝试:
将tkinter作为tkinter导入
导入tkinter.messagebox
除恐怖外:
Tkinter=无
#在Windows上,默认I/O模式为O_文本。将此设置为O_二进制
#避免对输入/输出流进行不必要的修改。
如果sys.platform==“win32”:
导入操作系统,msvcrt
msvcrt.setmode(sys.stdin.fileno(),os.O_二进制)
msvcrt.setmode(sys.stdout.fileno(),os.O_二进制)
#向webapp发送消息的助手函数。
def发送消息(消息):
#写入消息大小。
sys.stdout.buffer.write(结构包('I',len(消息)))
#写下信息本身。
系统标准输出写入(消息)
sys.stdout.flush()
#从webapp读取消息的线程。
def read_thread_func(队列):
消息编号=0
而1:
#读取消息长度(前4个字节)。
text\u length\u bytes=sys.stdin.buffer.read(4)
如果len(文本长度字节)==0:
如果队列:
queue.put(无)
系统出口(0)
#将消息长度解压缩为4字节整数。
text_length=struct.unpack('@I',text_length_字节)[0]
#阅读消息的文本(JSON对象)。
text=sys.stdin.buffer.read(文本长度)。解码('utf-8')
如果text='{“text”:“exit”}':
打破
如果队列:
queue.put(文本)
其他:
#在无头模式下,只需发送回显信息。
发送消息('{“echo”:%s}'%text)
如果Tkinter:
类NativeMessagingWindow(tkinter.Frame):
定义初始化(自我,队列):
self.queue=队列
tkinter.Frame.\uuuuu init\uuuuuuuu(自)
self.pack()
self.text=tkinter.text(self)
self.text.grid(行=0,列=0,padx=10,pady=10,列span=2)
self.text.config(state=tkinter.DISABLED,height=10,width=40)
self.messageContent=tkinter.StringVar()
self.sendEntry=tkinter.Entry(self,textvariable=self.messageContent)
self.sendEntry.grid(行=1,列=0,padx=10,pady=10)
self.sendButton=tkinter.Button(self,text=“Send”,command=self.onSend)
self.sendButton.grid(行=1,列=1,padx=10,pady=10)
船尾