当面の問題は、サンプル コードを正しく実行していないことです。より大きな問題は、Google がこのサンプル コードの使用方法に関する包括的なドキュメントを提供していないことです。
参照したネイティブ メッセージングの例は、Chrome 拡張機能のサンプル コードへのリンクのみです。調べてみると、ネイティブ メッセージング ホスト アプリケーションに関連するサンプル コードを見つけることができました。Chrome 拡張機能とネイティブ メッセージング ホスト アプリケーションの両方のサンプル コードを一緒に取得するには、nativeMessaging.zipをダウンロードします。その zip ファイルには、ネイティブ メッセージング ホストを Windows、Linux、および Mac OS X にインストールする方法に関する簡単な手順も記載されています。 Chrome 拡張機能。さらに、ネイティブ メッセージング ホストをインストールおよびアンインストールするためのスクリプトは、OS X ではそのままでは機能しません。私のインストール手順と修正されたスクリプトについては、以下を参照してください。
サンプル拡張機能とネイティブ ホスト アプリケーションのインストール方法
- nativeMessaging.zip ファイルをダウンロードして解凍します。
- Chrome 拡張機能をインストールする
- Chrome
chrome://extensions/
でアドレスバーに入力します
- 「パッケージ化されていない拡張機能を読み込む...」ボタンをクリックします
- 解凍したディレクトリに移動し、インポート
nativeMessaging
するディレクトリを選択しますapp
- ネイティブ メッセージング ホスト アプリケーションをインストールする
- OS X および Linux の場合、一部のファイルに実行権限を追加する必要があります。次のコマンドを実行します。
chmod a+rx nativeMessaging/host/install_host.sh nativeMessaging/host/native-messaging-example-host nativeMessaging/host/uninstall_host.sh
nativeMessaging/host/install_host.sh
OS X の場合、および のいくつかのバグを修正する必要がありますnativeMessaging/host/uninstall_host.sh
。修正されたスクリプトについては、以下を参照してください。
- OS X、Linux、および Windows の場合は、次の手順に従います。
nativeMessaging/README.txt
- Chrome 拡張機能を実行する
- Chrome
chrome://apps/
でアドレスバーに入力します
- Native Messaging Example アプリのアイコンをクリックします。
- アプリが読み込まれると、「接続」という名前のボタンが 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.