我认为它已经在等待答案,但不确定:
Native app does not work in Chrome extension
在Linux上,它工作正常,但在Windows 7和8上,我总是收到错误“未找到指定的本机消息传递主机”.
这是我的注册表(我已尝试使用双反斜杠和HKEY_LOCAL_MACHINE):
REG ADD HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo /ve /d C:\Users\Chriss\Desktop\nativeMessaging\host\com.google.chrome.example.echo-win.json
manifest.json的:
{
// Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
"key":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB","name": "Native Messaging Example","version": "1.0","manifest_version": 2,"description": "Send a message to a native application.","browser_action": {
"default_title": "Test Extension","default_popup": "main.html"
},"icons": {
"128": "icon-128.png"
},"permissions": [
"nativeMessaging"
]
}
com.google.chrome.example.echo-win.json:
{
"name": "com.google.chrome.example.echo","description": "Chrome Native Messaging API Example Host","path": "C:\Users\Chriss\Desktop\nativeMessaging\host\native-messaging-example-host.exe","type": "stdio","allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
本机的消息,例如,host.exe:
int main(int argc,char* argv[]) {
// Define our message
std::string message = "{\"text\": \"This is a response message\"}";
// Collect the length of the message
unsigned int len = message.length();
// We need to send the 4 bytes of length information
std::cout
<< char(((len >> 0) & 0xFF))
<< char(((len >> 8) & 0xFF))
<< char(((len >> 16) & 0xFF))
<< char(((len >> 24) & 0xFF));
// Now we can output our message
std::cout << message;
return 0;
}
JS片段(从http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/nativeMessaging/app/main.js?revision=228976开始):
function connect() {
var hostName = "com.google.chrome.example.echo";
appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.ondisconnect.addListener(ondisconnected);
updateUiState();
}
我无法找出问题所在.我的错在哪里?
更新
使用Procces Monitor监控注册表.我发现chrome.exe在64位密钥中搜索密钥.现在我可以看到没有丢失相关的注册表项,但我仍然得到错误.
我也在Windows上遇到这个问题,但是能够让它运行起来.请尝试以下方法:
关于注册表(我在HKLM,但HKCU应该没问题)你应该使用双反斜杠.这是我的.reg文件:
[HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\tcchrome.native.handler] @="G:\\\ExtChrome\\\Extension\\\Native\\\manifest.json"
>使用小写字母和仅3个部分保留本机进程的名称 – 即com.google.chrome.这听起来很奇怪,但这就是它对我有用的方式……
>将exe和清单放在同一个文件夹中,这样路径将是“native-messaging-example-host.exe” – 在这种情况下我肯定是因为我测试了它.
这是我的清单,例如:
{
"name": "chrome.native.handler","description": "BlaBla helper process","path": "chrome.native.handler.exe","allowed_origins": [
"chrome-extension://eoknpfoeckmeidbmbfoklejkppcnlfdm/"
]
}
顺便说一下,你没有正确处理回应.您应该以“本机字节顺序”发送消息长度 – 您正在执行的操作不适用于较大的消息.相反,你可以这样做:
cout.write((char*)&resSize,4); cout.write(responseBuf,resSize);
希望这可以帮助