我想它已经在等待答案了,但不确定: Native app does not work in Chrome extension
在Linux上,它工作得很好,但是在Windows 7和Windows 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.jsonmanifest.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/"
]
}native-messaging-example-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监视注册表之后。我发现chrome.exe在64位键中搜索密钥。现在,我可以看到,没有丢失相关的注册表项,但我仍然得到错误。
发布于 2014-02-24 15:44:16
在Windows上,我也在努力解决这个问题,但是我能够让它正常工作。尝试以下几点:
关于注册表(我的在HKLM,但HKCU应该可以),你应该使用双反斜杠。这是我的.reg文件:
[HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\tcchrome.native.handler]
@="G:\\\ExtChrome\\\Extension\\\Native\\\manifest.json"例如,这是我的舱单:
{
"name": "chrome.native.handler",
"description": "BlaBla helper process",
"path": "chrome.native.handler.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://eoknpfoeckmeidbmbfoklejkppcnlfdm/"
]
}顺便说一句,您没有正确地处理响应。您应该以“本机字节顺序”发送消息长度-您所做的工作将不适用于较大的消息。相反,您可以这样做:
cout.write((char*)&resSize, 4);
cout.write(responseBuf, resSize);希望这能有所帮助
https://stackoverflow.com/questions/21961775
复制相似问题