I did write an chrome extension that calls this connect() function to connect to a local C++ program:
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");
}
I can see the test1 in the Console, but afterwards I got the error
Uncaught TypeError: undefined is not a function
in the line
port = chrome.runtime.connectNative('com.a.chrome_interface');
My extensions manifest file is here:
{
"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
}
My com.a.chrome_interface.json looks like this:
{
"name": "com.a.chrome_interface",
"description": "Chrome Native Messaging API Example Host",
"path": "com.a.chrome_interface",
"type": "stdio",
"allowed_origins": [
"chrome-extension://abc.../"
]
}
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
/etc/opt/chrome/native-messaging-hosts/
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.
- As I'm writing an extension, do I have to use chrome.runtime.connectNative() or chrome.extension.connectNative()?
- How can I connect and send data to my C++ program?
test1 contentscript.js:17 Uncaught TypeError: undefined is not a function contentscript.js:19 connect contentscript.js:19
– VelezconnectNative()
is not available in a content script. But I just have this content script. Now I have to find out how to send data out of a content script. – Velez