我正在使用 Chrome 的本机消息功能实现网页和 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 messages的帮助。我似乎无法将“连接到本机应用程序的错误”与任何特别提到的错误联系起来。
我的扩展清单文件(“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
}
}