为什么我收到 Chrome 本机消息 "Specified native messaging host not found."?

Why am I getting Chrome Native Messaging "Specified native messaging host not found."?

我已经研究了一段时间了,我想不通。我已阅读 Chrome Native Messaging docs,但我不断从扩展中收到 "Specified native messaging host not found." 错误。

注册表指向的清单:

{
  "name": "com.fordcars.chromekeys",
  "description": "ChromeKeys description",
  "path": "C:\Users\fordcars\Desktop\Development\ChromeKeys\Debug\ChromeKeys.exe",
    "type": "stdio",
    "allowed_origins": [
     "chrome-extension://pdkakljppghagmaoijbpicogfdbodpbc"
     ]
}

扩展脚本:

// Event page

var nativeName = "com.fordcars.chromekeys";

var nativePort = chrome.runtime.connectNative(nativeName);

function nativeDataReceived(data)
{
    // Not used
}

function nativeDisconnected()
{
    console.log("Native program disconnected. Error: " + chrome.runtime.lastError.message);
}

nativePort.onMessage.addListener(nativeDataReceived);
nativePort.onDisconnect.addListener(nativeDisconnected);

请记住,我一连接 Native() 就收到错误消息。我的扩展清单中有 "nativeMessaging" 权限。

注册表:

Subkey: HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.fordcars.chromekeys

Value name: (Default)

Value: C:\Users\fordcars\Desktop\Development\ChromeKeys\Debug\nativeManifest.json

调试中: 我做了一些调试,发现如果我将 connectNative nativeName 从 com.fordcars.chromekeys 更改为其他任何内容,我 still 得到相同的结果错误,所以要么是找不到注册表项or/and我没有好的manifest.json

谢谢!

反斜杠是 JSON 中的转义字符。您必须在清单中使用两个反斜杠作为路径分隔符:

// BAD:
"path": "C:\Users\fordcars\Desktop\Development\ChromeKeys\Debug\ChromeKeys.exe",
// GOOD:
"path": "C:\Users\fordcars\Desktop\Development\ChromeKeys\Debug\ChromeKeys.exe",

我强烈建议学习如何按照 Debugging native messaging 中的描述进行调试,因为如果你这样做,那么你会看到更详细的错误消息:

Found manifest, but not the binary for native messaging host com.fordcars.chromekeys. Host path specified in the manifest: C:UsersordcarsDesktopDevelopmentChromeKeysDebugChromeKeys.exe

即使不查看错误日志,如果您按照错误消息中的要点进行操作,您也会注意到 sample manifest 和您的(即拼写错误的反斜杠)之间的区别。