Chrome 扩展 NativeMessaging 'connectNative' 未定义

问题描述 投票:0回答:2

我正在尝试使用runtime.connectNative和postMessage来实现chrome扩展。我遵循 chrome 文档,下载了 本机消息传递示例,我尝试在不进行任何更改的情况下运行该示例,而本机主机应用程序的代码可以在 here 找到。

但是,我收到错误: 未捕获的类型错误:无法读取未定义的属性“connectNative”。

错误是由 javascript 扩展文件触发的,在这一行中:
端口 = chrome.runtime.connectNative(主机名);

当从清单加载扩展时,如下所示:

"app": {
   "launch": {
      "local_path": "main.html"
   }
}

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

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

javascript google-chrome-extension chrome-native-messaging
2个回答
8
投票

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

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

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

  1. 下载并解压nativeMessaging.zip 文件。
  2. 安装 Chrome 扩展程序
  3. 在 Chrome 中,在地址栏中输入
    chrome://extensions/
  4. 单击“加载解压的扩展...”按钮
  5. 导航到解压后的
    nativeMessaging
    目录并选择
    app
    目录进行导入
  6. 安装本机消息传递主机应用程序
  7. 对于 OS X 和 Linux,您需要为某些文件添加执行权限。运行命令:
    chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.sh
  8. 对于 OS X,您需要修复
    nativeMessaging/host/install_host.sh
    nativeMessaging/host/uninstall_host.sh
    中的一些错误。请参阅下面的更正脚本。
  9. 对于 OS X、Linux 和 Windows,请遵循
    nativeMessaging/README.txt
  10. 中的说明
  11. 运行 Chrome 扩展程序
  12. 在 Chrome 中,在地址栏中输入
    chrome://apps/
  13. 单击本机消息传递示例应用程序图标
  14. 应用程序加载后,您应该会看到一个名为“连接”的按钮。单击该按钮,您应该会看到本机消息传递主机应用程序自动启动。

已更正

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.

1
投票

我想提供一个 python 3 版本的脚本来替换 native-messaging-example-host。它已使用 Chrome v86 进行测试并按预期工作。请注意,当 tkinter 窗口关闭时,Python 内核崩溃 - 这是因为threading内部的二进制数据交换导致线程被硬锁定(更多信息here)。我添加了一个从 chrome 应用程序发送的命令 exit 以停止线程等待另一个标准输入。收到后,退出时python不会崩溃。

Python 3版本(使用3.7.4测试):

# A simple native messaging host. Shows a Tkinter dialog with incoming messages
# that also allows to send message back to the webapp.

import struct
import sys
import threading
import queue as Queue

try:
  import tkinter as Tkinter
  import tkinter.messagebox
except ImportError:
  Tkinter = None

# On Windows, the default I/O mode is O_TEXT. Set this to O_BINARY
# to avoid unwanted modifications of the input/output streams.
if sys.platform == "win32":
  import os, msvcrt
  msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
  msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

# Helper function that sends a message to the webapp.
def send_message(message):
   # Write message size.
  sys.stdout.buffer.write(struct.pack('I', len(message)))
  # Write the message itself.
  sys.stdout.write(message)
  sys.stdout.flush()

# Thread that reads messages from the webapp.
def read_thread_func(queue):
  message_number = 0
  while 1:
    # Read the message length (first 4 bytes).
    text_length_bytes = sys.stdin.buffer.read(4)

    if len(text_length_bytes) == 0:
      if queue:
        queue.put(None)
      sys.exit(0)

    # Unpack message length as 4 byte integer.
    text_length = struct.unpack('@I', text_length_bytes)[0]

    # Read the text (JSON object) of the message.
    text = sys.stdin.buffer.read(text_length).decode('utf-8')

    if text == '{"text":"exit"}':
      break

    if queue:
      queue.put(text)
    else:
      # In headless mode just send an echo message back.
      send_message('{"echo": %s}' % text)

if Tkinter:
  class NativeMessagingWindow(tkinter.Frame):
    def __init__(self, queue):
      self.queue = queue

      tkinter.Frame.__init__(self)
      self.pack()

      self.text = tkinter.Text(self)
      self.text.grid(row=0, column=0, padx=10, pady=10, columnspan=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(row=1, column=0, padx=10, pady=10)

      self.sendButton = tkinter.Button(self, text="Send", command=self.onSend)
      self.sendButton.grid(row=1, column=1, padx=10, pady=10)

      self.after(100, self.processMessages)

    def processMessages(self):
      while not self.queue.empty():
        message = self.queue.get_nowait()
        if message == None:
          self.quit()
          return
        self.log("Received %s" % message)

      self.after(100, self.processMessages)

    def onSend(self):
      text = '{"text": "' + self.messageContent.get() + '"}'
      self.log('Sending %s' % text)
      try:
        send_message(text)
      except IOError:
        tkinter.messagebox.showinfo('Native Messaging Example',
                              'Failed to send message.')
        sys.exit(1)

    def log(self, message):
      self.text.config(state=tkinter.NORMAL)
      self.text.insert(tkinter.END, message + "\n")
      self.text.config(state=tkinter.DISABLED)


def Main():
  if not Tkinter:
    send_message('"Tkinter python module wasn\'t found. Running in headless ' +
                 'mode. Please consider installing Tkinter."')
    read_thread_func(None)
    sys.exit(0)

  queue = Queue.Queue()

  main_window = NativeMessagingWindow(queue)
  main_window.master.title('Native Messaging Example')

  thread = threading.Thread(target=read_thread_func, args=(queue,))
  thread.daemon = True
  thread.start()

  main_window.mainloop()

  sys.exit(0)


if __name__ == '__main__':
  Main()

免责声明:我使用 2to3 实用程序初始转换为 python 3。我还采用了 webextensions (firefox) 版本的 nativeMessage API 示例的更改(它经过简化,不使用 tkinter gui)。

© www.soinside.com 2019 - 2024. All rights reserved.