Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.runtime.connectNative generates Uncaught TypeError: undefined is not a function

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.

  1. As I'm writing an extension, do I have to use chrome.runtime.connectNative() or chrome.extension.connectNative()?
  2. How can I connect and send data to my C++ program?
like image 925
EmbeddedDesign Avatar asked Jul 13 '14 18:07

EmbeddedDesign


1 Answers

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:

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 as above in my question but additionaly:

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

com.a.chrome_interface.json is unchange as in the question above.

like image 85
EmbeddedDesign Avatar answered Nov 09 '22 14:11

EmbeddedDesign