问题
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