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

戏子无情 提交于 2020-01-24 23:27:00

问题


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??


回答1:


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).



来源:https://stackoverflow.com/questions/27726799/chrome-native-messaging-why-am-i-receiving-a-specified-native-messaging-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!