繁体   English   中英

Chrome 本地消息传递 — 为什么我会收到“未找到指定的本地消息传递主机”错误?

[英]Chrome Native Messaging — Why am I receiving a “Specified native messaging host not found” error?

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

Specified native messaging host not found.

为什么要这样做? 生成 msg 的侦听器如下所示:

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

文档底部有一个关于此特定错误的完整部分 ( Native Messaging ),并且建议的补救措施说明清单文件的命名、放置或定义 (JSON) 不正确,或者主机应用程序未命名或定位清单说它应该在哪里。 该文档说 connectNative() 将“在单独的进程中启动主机”,但活动监视器没有提供本地主机应用程序已启动的证据。

我调用 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);
    }
});

我的本机主机清单文件根据文档位于正确的文件夹中,解析为 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 扩展程序也需要一个清单,在我正确设置权限部分之前,我无法从 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

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

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

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

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

~/Library/Application Support/Chromium/NativeMessagingHosts/

(Mac 当前记录的路径不正确( patch )。不过install.sh 中的路径(来自文档中的示例)是正确的)。

只是想提一下,如果您使用的是不同的 Chrome 发布渠道,例如 Canary,这在开发过程中很常见,您将不得不相应地调整路径。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM