【发布时间】: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。
- 在编写扩展程序时,是否必须使用 chrome.runtime.connectNative() 或 chrome.extension.connectNative()?
- 如何连接并发送数据到我的 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