根据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;
}
在文档的底部有一整节关于这个特定错误的内容(本机消息传递),并且建议的补救措施说,要么清单文件的命名、放置或定义(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
很明显它找不到主机清单,但是为什么呢??
发布于 2015-01-03 18:15:11
对于Google Chrome,清单文件的系统范围目录为:
~/Library/Application Support/Google/Chrome/NativeMessagingHosts/
特定于用户的安装路径位于:
~/Library/Application Support/Chromium/NativeMessagingHosts/
(当前documentedMac的路径不正确(patch)。中的路径install.sh(来自榜样在文档中)是正确的)。
https://stackoverflow.com/questions/27726799
复制相似问题