4

runtime.connectNative と postMessage を使用してクロム拡張機能を実装しようとしています。私はクロムのドキュメントに従っており、変更なしで実行しようとしているネイティブメッセージングの例をダウンロードしましたが、ネイティブホストアプリケーションのコードはここにあります。

ただし、次のエラーが表示されます: Uncaught TypeError: 未定義のプロパティ 'connectNative' を読み取ることができません。

エラーは、次の行の javascript 拡張ファイルからトリガーされています:
port = chrome.runtime.connectNative(hostName);

拡張機能が次のようにマニフェストから読み込まれている間:

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

問題を解決する方法はありますか?

Chrome バージョン 34、Windows 7、8.1 でテスト済み

4

2 に答える 2

8

当面の問題は、サンプル コードを正しく実行していないことです。より大きな問題は、Google がこのサンプル コードの使用方法に関する包括的なドキュメントを提供していないことです。

参照したネイティブ メッセージングの例は、Chrome 拡張機能のサンプル コードへのリンクのみです。調べてみると、ネイティブ メッセージング ホスト アプリケーションに関連するサンプル コードを見つけることができました。Chrome 拡張機能とネイティブ メッセージング ホスト アプリケーションの両方のサンプル コードを一緒に取得するには、nativeMessaging.zipをダウンロードします。その zip ファイルには、ネイティブ メッセージング ホストを Windows、Linux、および Mac OS X にインストールする方法に関する簡単な手順も記載されています。 Chrome 拡張機能。さらに、ネイティブ メッセージング ホストをインストールおよびアンインストールするためのスクリプトは、OS X ではそのままでは機能しません。私のインストール手順と修正されたスクリプトについては、以下を参照してください。

サンプル拡張機能とネイティブ ホスト アプリケーションのインストール方法

  1. nativeMessaging.zip ファイルをダウンロードして解凍します。
  2. Chrome 拡張機能をインストールする
    1. Chromechrome://extensions/でアドレスバーに入力します
    2. 「パッケージ化されていない拡張機能を読み込む...」ボタンをクリックします
    3. 解凍したディレクトリに移動し、インポートnativeMessagingするディレクトリを選択しますapp
  3. ネイティブ メッセージング ホスト アプリケーションをインストールする
    1. OS X および Linux の場合、一部のファイルに実行権限を追加する必要があります。次のコマンドを実行します。chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.sh
    2. nativeMessaging/host/install_host.shOS X の場合、および のいくつかのバグを修正する必要がありますnativeMessaging/host/uninstall_host.sh。修正されたスクリプトについては、以下を参照してください。
    3. OS X、Linux、および Windows の場合は、次の手順に従います。nativeMessaging/README.txt
  4. Chrome 拡張機能を実行する
    1. Chromechrome://apps/でアドレスバーに入力します
    2. Native Messaging Example アプリのアイコンをクリックします。
    3. アプリが読み込まれると、「接続」という名前のボタンが 1 つ表示されます。そのボタンをクリックすると、ネイティブ メッセージング ホスト アプリケーションが自動的に起動します。

修正済み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.
于 2014-08-07T23:35:53.003 に答える