简体   繁体   中英

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

According to the Chrome Native Messaging docs a successful call to connectNative() returns a port, with which you can post messages to a native app (a Mac app). In my case nativeConnect() does return a valid port in my case, but a call to onDisconnected() listener is fired almost immediately. Whenever the listener is fired it prints the "lastError" property to the browser's console, and this gives:

Specified native messaging host not found.

Why is it doing this? The listener that produces the msg looks like this:

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

There's an entire section on this particular error toward the bottom of the docs ( Native Messaging ), and the proposed remedies say that either the manifest file is named, placed or defined (JSON) incorrectly, or else the host app is not named or located where the manifest says it should be. The doc says connectNative() will "start the host in a separate process", but Activity Monitor gives no evidence that the native host app was launched.

I call connectNative() as follows:

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);
    }
});

My native host manifest file is located in the correct folder as per docs, parses fine as JSON, and looks like:

{
  "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/"]
}

The Chrome extension requires a manifest also, and until I got the permissions section right I was unable to get a non-null port back from connectNative(), so I'm pretty sure this is now correct:

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

UPDATE:

Figured out how to launch the Chrome browser from Mac's Terminal with flags enabling viewing of more "verbose" logging. Then when I ran things I noticed this output:

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

Pretty clear it cannot find the host manifest, but why??

For Google Chrome, the system-wide directory for the manifest file is:

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

The user-specific install path is at:

~/Library/Application Support/Chromium/NativeMessagingHosts/

(the currently documented path for Mac is incorrect ( patch ). The paths in install.sh (from the example in the documentation) are correct though).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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