chrome.runtime.connectNative生成Uncaught TypeError:undefined不是一个函数 [英] chrome.runtime.connectNative generates Uncaught TypeError: undefined is not a function
问题描述
我编写了一个chrome扩展,它调用connect()函数连接到本地C ++程序:
函数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不是函数
在线
port = chrome.runtime.connectNative('com.a。 chrome_interface');
我的扩展清单文件在这里:
{
name:CPP_Connect,
version:1.0,
description:发送数据到CPP程序 ,
content_scripts:[
{
matches:[< all_urls>],
js:[contentscript.js ]
}
],
权限:[contextMenus,tabs,nativeMessaging,< all_urls>],
manifest_version:2
}
我的com.a .chrome_interface.json看起来像这样:
{
name:com.a.chrome_interface,
description:Chrome原生消息API示例主机,
路径: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()脚本运行低谷,并且错误消息disapear,但没有数据到达我的C ++程序。
我读过并尝试按照
https://developer.chrome.com/extensions / messaging#native-messaging
并搜索了很多内容,但我可以找出原因。
我使用Chromium 34在Ubuntu 12.04上。
- 在编写扩展时,是否必须使用chrome.runtime.connectNative()或chrome.extension.connectNative() ?
- 如何连接数据并将数据发送到我的C ++程序?
contentscript.js: background.js: manifest.json与我上面的问题一样,但是另外: I did write an chrome extension that calls this connect() function to connect to a local C++ program: I can see the test1 in the Console, but afterwards I got the error in the line My extensions manifest file is here: My com.a.chrome_interface.json looks like this: and com.a.chrome_interface is a linux executable C++ file that generates a file, if it is called and this file is never created.
I did put both files in So I guess, I did register my C++ correctly but I also guess, if I would register it wrong, I should get a different error.
If I use chrome.extension.connect() the script runs trough and the error message disapear but no data arrive in my C++ program. I did read and try to follow instructions on
https://developer.chrome.com/extensions/messaging#native-messaging
and googled a lot but I could find out the reason of my problem. I'm using Chromium 34 on Ubuntu 12.04.
contentscript.js: background.js: manifest.json as above in my question but additionaly: 这篇关于chrome.runtime.connectNative生成Uncaught TypeError:undefined不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
要连接到本地程序,内容脚本必须发送数据,例如到扩展的后台脚本和后台脚本中,可以使用
port = chrome.extension.connectNative
。
所以这里有一个解决方案:
....
//发送数据到后台脚本
chrome.extension.sendRequest(Some Data);
....
函数connect(){
//连接到本地程序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);
}
});
...
background:{
scripts:[background.js]
},
...
com.a.chrome_interface.json
在上面的问题中不变。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");
}
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
}
{
"name": "com.a.chrome_interface",
"description": "Chrome Native Messaging API Example Host",
"path": "com.a.chrome_interface",
"type": "stdio",
"allowed_origins": [
"chrome-extension://abc.../"
]
}
/etc/opt/chrome/native-messaging-hosts/
connectNative()
is not available in a content scripts.
To connect to a local program the content script must send the data e.g. to the background script of the extension and in the background script,
port = chrome.extension.connectNative
can be used.
So here a solution:....
// send data to background script
chrome.extension.sendRequest("Some Data");
....
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);
}
});
...
"background": {
"scripts": ["background.js"]
},
...
com.a.chrome_interface.json
is unchange as in the question above.