使用Chrome扩展程序中的connectNative()连接到本机主机应用程序时,导致“连接到本机应用程序时出错”的原因是什么?

时间:2014-12-30 21:30:52

标签: javascript google-chrome google-chrome-extension chrome-native-messaging

我正在使用Chrome的Native Messaging功能在网页和Mac应用程序之间实现通信。在我对chrome.runtime.connectNative(“my_native_host_app_name”)进行javascript调用之前,似乎一切顺利,这会在控制台中产生以下错误:

Error in event handler for runtime.onMessageExternal: Error connecting to native app: com.allinlearning.nmhforbrowserextension
Stack trace: Error: Error connecting to native app: com.allinlearning.nmhforbrowserextension
    at Object.<anonymous> (extensions::runtime:189:11)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Object.handleRequest (extensions::binding:55:27)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Object.<anonymous> (extensions::binding:318:32)
    at chrome-extension://gldheanjpgopipommeingjlnoiamdfol/background.js:19:27
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at EventImpl.dispatchToListener (extensions::event_bindings:395:22)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.$Array.forEach.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:65:26) extensions::event_bindings:383
EventImpl.dispatch_extensions::event_bindings:383
EventImpl.dispatchextensions::event_bindings:401
$Array.forEach.publicClass.(anonymous function)extensions::utils:65
messageListenerextensions::messaging:190
EventImpl.dispatchToListenerextensions::event_bindings:395
$Array.forEach.publicClass.(anonymous function)extensions::utils:65
EventImpl.dispatch_extensions::event_bindings:378
EventImpl.dispatchextensions::event_bindings:401
$Array.forEach.publicClass.(anonymous function)extensions::utils:65
dispatchOnMessageextensions::messaging:304

似乎导致此错误的实际调用(参考堆栈跟踪中的background.js中的第19行)是:

port = chrome.runtime.connectNative("com.nmhforbrowserextension");

要提供更多上下文,可以从侦听器调用它:

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");
    else
       console.log("connectNative() returned null for the port");
});

它永远不会到达if语句。我主要使用的文档是Chrome Native Messaging。在文档的底部,它有一个部分来提供常见错误的帮助Debugging native messaging。我似乎无法将“连接到本机应用程序时出错”与任何特别提到的错误联系起来。

我的扩展清单文件(“manifest.json”)的完整内容是:

{
  "manifest_version": 2,

    "name": "AIL Extension",
    "version": "1.0",
    "description": "New Reader",

    "background": {
        "scripts": ["background.js"]
     },

    "externally_connectable": {
    // Extension and app IDs. If this field is not specified, no
    // extensions or apps can connect.
    "ids": [
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
      "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
    ],
    // Match patterns for web pages. Does not affect content scripts.
    // If this field is not specified, no webpages can connect.
    "matches": [
      "http://localhost/charles/qrexttest/*"
    ],

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

    // Indicates that the extension would like to make use of the TLS
    // channel ID of the web page connecting to it. The web page must
    // also opt to send the TLS channel ID to the extension via setting
    // includeTlsChannelId to true in runtime.connect's connectInfo
    // or runtime.sendMessage's options.
    "accepts_tls_channel_id": false
  }
}

1 个答案:

答案 0 :(得分:2)

“连接到本机应用程序时出错:[本机消息传递主机ID]”的一个可能原因是缺少必需的nativeMessaging权限。

Declare清单文件中的"nativeMessaging"权限,重新加载扩展程序并重试。

chrome.runtime.connectNativesendNativeMessage未经此权限is a bug可用的事实。)