0

Chrome 拡張機能用の Chrome Native Messaging API を試しています。

ネイティブ アプリの Manifest.json:

{
  "name": "app.native",
  "description": "Native Message API Test.",
  "path": "native.exe",
  "type": "stdio",
  "allowed_origins": ["chrome-extension://kembignchdjhopkkcolnamikcenaocdm/"]
}

Windows レジストリ値:

HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\app.native=D:\connectNative\manifest.json

私も試しましたD:\\\\connectNative\\\\manifest.json

そして、Chrome 拡張機能 manifest.json の「権限」に「nativeMessaging」を追加します。

ネイティブ アプリの cpp:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]) {
    string input = "";
    string message="{\"text\": \"This is a response message\",\"num\": \"three\"}";
    unsigned int len = message.length();
    cout << char(((len>>0) & 0xFF))
         << char(((len>>8) & 0xFF))
         << char(((len>>16) & 0xFF))
         << char(((len>>24) & 0xFF));
    cout << message <<endl;
    getline(cin, input);
    cout << "You entered: " << input << endl;
    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile << input;
    myfile.close();

    return 0;
}

すべてが完了したら、Chrome 拡張機能を試します。

var testport = chrome.runtime.connectNative('app.native');
testport.onMessage.addListener(function(msg) {
    console.log("Received" + msg);
});
testport.onDisconnect.addListener(function() {
  console.log("Disconnected");
});

メッセージを受信できず、常に「切断されました」と出力されます。

存在しないアプリに接続しようとしましたが、それでも「切断されました」と出力されるため、このネイティブ アプリが正しく構成されていないことがわかります。

誰が間違っているか、または私が見逃したものを指摘できますか?

4

2 に答える 2