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

1

12

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?
Velez answered 13/7, 2014 at 18:42 Comment(6)
Can you post the full traceback for that error?Overlay
Is that what you are asking for? test1 contentscript.js:17 Uncaught TypeError: undefined is not a function contentscript.js:19 connect contentscript.js:19Velez
Now I did find out that connectNative() 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
@Velez You should post that as an answer ("my problem was that it's unavailable from a content script"). As for sending data out, you need regular Messaging and a background page that acts like a proxy for your data.Glissando
@Xan: Thanks for your feedback. Now I did. Is there a reason why you did not write your answer as an answer?Velez
@Velez You figured it out yourself; you deserve the reputation from the self-answer.Glissando
V
18

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.

Velez answered 14/7, 2014 at 18:20 Comment(2)
Doesn't work. Using your code where chrome.extension.connectNative is in the background script, the error still shows i.sstatic.net/BMB9s.pngHarrietharriett
Do not forget "permissions": [ "nativeMessaging" ],"Monophagous

© 2022 - 2024 — McMap. All rights reserved.