【问题标题】:chrome.runtime.connectNative generates Uncaught TypeError: undefined is not a functionchrome.runtime.connectNative 生成​​ Uncaught TypeError: undefined is not a function
【发布时间】:2014-09-03 17:59:00
【问题描述】:

我确实编写了一个 chrome 扩展,它调用这个 connect() 函数来连接到本地 C++ 程序:

function connect() {
  console.log("test1");
  //port = chrome.extension.connectNative('com.a.chrome_interface');
  port = chrome.runtime.connectNative('com.a.chrome_interface');

  port.onMessage.addListener(onNativeMessage);
  port.onDisconnect.addListener(onDisconnected);
  console.log("test5");
}

我可以在控制台中看到 test1,但后来我得到了错误

Uncaught TypeError: undefined is not a function

排队

port = chrome.runtime.connectNative('com.a.chrome_interface');

我的扩展清单文件在这里:

{
  "name": "CPP_Connect",
  "version": "1.0",
  "description": "Send data to CPP program",

  "content_scripts": [
   {
     "matches": ["<all_urls>"],
     "js": ["contentscript.js"]
   }
  ],

  "permissions": ["contextMenus", "tabs", "nativeMessaging", "<all_urls>"],

  "manifest_version": 2

}

我的 com.a.chrome_interface.json 看起来像这样:

{
"name": "com.a.chrome_interface",
"description": "Chrome Native Messaging API Example Host",
"path": "com.a.chrome_interface",
"type": "stdio",
"allowed_origins": [
"chrome-extension://abc.../"
]
}

并且 com.a.chrome_interface 是一个 linux 可执行 C++ 文件,如果它被调用并且这个文件永远不会被创建,它会生成一个文件。 我确实将两个文件都放入了

 /etc/opt/chrome/native-messaging-hosts/

所以我想,我确实正确注册了我的 C++,但我也猜想,如果我注册错了,我应该会得到一个不同的错误。 如果我使用 chrome.extension.connect() 脚本运行低谷并且错误消息消失但没有数据到达我的 C++ 程序。

我确实阅读并尝试按照以下说明进行操作 https://developer.chrome.com/extensions/messaging#native-messaging 并搜索了很多,但我可以找出问题的原因。

我在 Ubuntu 12.04 上使用 Chromium 34。

  1. 在编写扩展程序时,是否必须使用 chrome.runtime.connectNative() 或 chrome.extension.connectNative()?
  2. 如何连接并发送数据到我的 C++ 程序?

【问题讨论】:

  • 您能发布该错误的完整回溯吗?
  • 这是您要求的吗? test1 contentscript.js:17 Uncaught TypeError: undefined is not a function contentscript.js:19 connect contentscript.js:19
  • 现在我发现 connectNative() 在内容脚本中不可用。但我只有这个内容脚本。现在我必须弄清楚如何从内容脚本中发送数据。
  • @EmbeddedDesign 您应该将其发布为答案(“我的问题是内容脚本无法使用它”)。至于发送数据,您需要常规的消息传递和一个充当数据代理的后台页面。
  • @Xan:感谢您的反馈。现在我做到了。您没有将答案写成答案有什么原因吗?

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


【解决方案1】:

connectNative() 在内容脚本中不可用。 要连接到本地程序,内容脚本必须发送数据,例如到扩展的后台脚本和后台脚本中, port = chrome.extension.connectNative 可以使用。 所以这里有一个解决方案:

contentscript.js:

....
// send data to background script
chrome.extension.sendRequest("Some Data");
....

background.js:

function connect() {
    // connect to local program com.a.chrome_interface
    port = chrome.extension.connectNative('com.a.chrome_interface');
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
}

chrome.extension.onRequest.addListener(function(data, sender) {
    if (data.length > 0) {
        connect();
        sendNativeMessage(data);
    }
});

manifest.json 在我的问题中如上所述,但另外:

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

com.a.chrome_interface.json 与上述问题相同。

【讨论】:

  • 不起作用。在后台脚本中使用chrome.extension.connectNative 的代码,错误仍然显示i.stack.imgur.com/BMB9s.png
  • 不要忘记“权限”:[“nativeMessaging”],"
猜你喜欢
  • 1970-01-01
  • 2014-08-15
  • 2015-01-14
  • 2014-07-06
  • 2014-09-01
  • 2015-11-04
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多