Chrome 扩展 NativeMessaging 'connectNative' 未定义 [英] Chrome Extension NativeMessaging 'connectNative' undefined

查看:24
本文介绍了Chrome 扩展 NativeMessaging 'connectNative' 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 runtime.connectNative 和 postMessage 实现 chrome 扩展.我正在关注 chrome 文档,下载了 本机消息传递示例,我试图在没有任何更改的情况下运行它,而本机主机应用程序的代码可以在 此处.

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.

但是,我收到错误消息:未捕获的类型错误:无法读取未定义的属性connectNative".

However, I'm getting the error: Uncaught TypeError: Cannot read property 'connectNative' of undefined.

错误是从 javascript 扩展文件触发的,在这一行:
端口 = chrome.runtime.connectNative(hostName);

The error is being triggered from the javascript extension file, in this line:
port = chrome.runtime.connectNative(hostName);

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

while the extension is being loaded from the manifest like so:

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

请问有什么办法可以解决问题吗?

Any ideas how to solve the problem please?

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

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.

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

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.

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

  1. 下载并解压缩 nativeMessaging.zip 文件.
  2. 安装 Chrome 扩展程序
  1. Download and unzip the nativeMessaging.zip file.
  2. Install the Chrome extension
  1. 在 Chrome 中的地址栏中输入 chrome://extensions/
  2. 点击加载解压后的扩展..."按钮
  3. 导航到解压后的 nativeMessaging 目录并选择 app 目录进行导入
  1. In Chrome enter chrome://extensions/ in the address bar
  2. Click the "Load unpacked extension..." button
  3. Navigate to the unzipped nativeMessaging directory and select the app directory for import

  • 安装本机消息传递主机应用程序

  • Install the native messaging host application

    1. 对于 OS X 和 Linux,您需要为某些文件添加执行权限.运行命令:chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.sh
    2. 对于 OS X,您需要修复 nativeMessaging/host/install_host.shnativeMessaging/host/uninstall_host.sh 中的一些错误.有关更正后的脚本,请参见下文.
    3. 对于 OS X、Linux 和 Windows,请按照 nativeMessaging/README.txt
    4. 中的说明进行操作
    1. 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
    2. For OS X you’ll need to fix some bugs in nativeMessaging/host/install_host.sh and nativeMessaging/host/uninstall_host.sh. See below for the corrected scripts.
    3. For OS X, Linux and Windows follow the instructions in nativeMessaging/README.txt

  • 运行 Chrome 扩展程序

  • Run the Chrome extension

    1. 在 Chrome 中的地址栏中输入 chrome://apps/
    2. 点击本机消息传递示例应用图标
    3. 应用加载后,您应该会看到一个名为连接"的按钮.单击该按钮,您应该会看到本机消息传递主机应用程序自动启动.

  • 更正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.
    

    这篇关于Chrome 扩展 NativeMessaging 'connectNative' 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆