我正在尝试用于 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
我将“nativeMessaging”添加到 Chrome 扩展 manifest.json 中的“权限”中。
本机应用程序 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");
});
它无法接收任何消息并始终打印“已断开连接”。
我尝试连接到一个不存在的应用程序,它仍然打印“断开连接”,所以我知道这个本机应用程序配置不正确。
谁能指出什么是错的或我错过了什么?