I am trying Chrome Native Messaging API for Chrome extension.
Manifest.json for native app:
{
"name": "app.native",
"description": "Native Message API Test.",
"path": "native.exe",
"type": "stdio",
"allowed_origins": ["chrome-extension://kembignchdjhopkkcolnamikcenaocdm/"]
}
Windows Registry value:
HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\app.native=D:\connectNative\manifest.json
I also tried D:\\\\connectNative\\\\manifest.json
And I add "nativeMessaging" to "permissions" in Chrome extension manifest.json.
Native app 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;
}
After all is done, i try in my Chrome extension:
var testport = chrome.runtime.connectNative('app.native');
testport.onMessage.addListener(function(msg) {
console.log("Received" + msg);
});
testport.onDisconnect.addListener(function() {
console.log("Disconnected");
});
It cannot receive any message and always print "Disconnected".
I try to connect to a non-existing app, it still print "Disconnected", so I know this native app is not configured right.
Can anyone point out what is wrong or what i missed?