javascript - Chrome Native Messaging - 为什么我会收到 "Specified native messaging host not found"错误?

标签 javascript google-chrome google-chrome-extension chrome-native-messaging

根据 Chrome Native Messaging 文档,成功调用 connectNative() 会返回一个端口,您可以使用该端口将消息发布到 native 应用程序(Mac 应用程序)。在我的例子中,nativeConnect()确实返回了一个有效的端口,但是对onDisconnected()监听器的调用几乎立即被触发。每当监听器被触发时,它都会将“lastError”属性打印到浏览器的控制台,这会给出:

Specified native messaging host not found.

为什么要这样做?生成消息的监听器如下所示:

function onDisconnected() {
  console.log("Inside onDisconnected(): " + chrome.runtime.lastError.message);
  port = null;
}

文档底部有一整节关于此特定错误的内容 ( Native Messaging ),并且建议的补救措施指出, list 文件的命名、放置或定义 (JSON) 不正确,或者主机应用程序未命名或位于 list 规定的位置。该文档说 connectNative() 将“在单独的进程中启动主机”,但事件监视器没有提供任何证据表明 native 主机应用程序已启动。

我调用 connectNative() 如下:

chrome.runtime.onMessageExternal.addListener(

  function(request, sender, sendResponse) {
    //var imgdata = JSON.stringify(request.imgdata);
    //process it somehow here

    port = chrome.runtime.connectNative("com.allinlearning.nmhforbrowserextension");

    if (port)
    {
       console.log("connectNative() returned a non-null port");

       port.onMessage.addListener(onNativeMessage);
       port.onDisconnect.addListener(onDisconnected);
    }
});

我的本​​机主机 list 文件按照文档位于正确的文件夹中,可以很好地解析为 JSON,并且看起来像:

{
  "name": "com.allinlearning.nmhforbrowserextension",
  "description": "Manifest for native messaging host for Google browser extension",
  "path": "/Users/mycomputer1/Documents/nmhost.app",
  "type": "stdio",
  "allowed_origins": ["chrome-extension://gldheanjpgopipommeingjlnoiamdfol/"]
}

Chrome 扩展程序还需要一个 list ,在我获得正确的权限部分之前,我无法从 connectNative() 获取非空端口,所以我很确定现在这是正确的:

"permissions": [
               "nativeMessaging",
                "tabs",
                "activeTab",
                "background",
                "http://*/", "https://*/"
                ]

更新:

弄清楚如何从 Mac 终端启动 Chrome 浏览器,并带有允许查看更多“详细”日志记录的标志。然后当我运行时我注意到这个输出:

[21285:38915:1231/164417:ERROR:native_process_launcher.cc(131)] Can't find manifest for native messaging host com.allinlearning.nmhforbrowserextension

很明显它找不到主机 list ,但为什么??

最佳答案

对于 Google Chrome, list 文件的系统范围目录是:

~/Library/Application Support/Google/Chrome/NativeMessagingHosts/

用户特定的安装路径位于:

~/Library/Application Support/Chromium/NativeMessagingHosts/

(当前 Mac 的 documented 路径不正确 ( patch )。install.sh 中的路径(来自文档中的 the example )是正确的)。

关于javascript - Chrome Native Messaging - 为什么我会收到 "Specified native messaging host not found"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27726799/

相关文章:

javascript - 使用另一个 div 中的链接更改 div 内容

reactjs - 如何生成有效的 arkose_token 来创建 GPT-4 对话?

google-chrome - 从网页激活 Chrome 应用程序?

javascript - Firefox 中的大子字符串比 Chrome 快约 9000 倍 : why?

javascript - 无法在 Chrome 网络选项卡中看到 1000 万个字符长度的请求负载

javascript - 捕获给定类型的*所有*事件,无论气泡/目标如何

google-chrome - 带有 NaCl 扩展的音频效果

javascript - 使用 javascript(无 jQuery)将第一个元素之后的所有元素添加到 div

javascript - 如何在 Angular 上监听 ag-Grid 的 sortChange(event)?

javascript - 如何使谷歌地图在任何 div 元素内响应?