Vikram Ranabhatt
Vikram Ranabhatt

Reputation: 7620

Uncaught TypeError: Cannot call method 'connectNative' in Native Messaging App

I am working on native messaging app.I have created following files

1.C++ conole app 2.JS file 3. manifet file I have created registry entry like this enter image description here

Now I am getting error in the line port = chrome.runtime.connectNative(hostName);.I noticed chrome.runtime itself is undefined.let me know am I missing anything here.

Manifest.jason

function connect() {
  //var m1 = chrome.runtime.getManifest();
  var hostName = "NM1";
  var t1 = chrome;
  appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")    

  port = chrome.runtime.connectNative(hostName);
  port.onMessage.addListener(onNativeMessage);
  port.onDisconnect.addListener(onDisconnected);
  updateUiState();
}

main.js

function connect() {
  //var m1 = chrome.runtime.getManifest();
  var hostName = "NM1";  
  appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")

  port = chrome.runtime.connectNative(hostName);
  port.onMessage.addListener(onNativeMessage);
  port.onDisconnect.addListener(onDisconnected);
  updateUiState();
}

Upvotes: 1

Views: 1394

Answers (1)

Kirk Backus
Kirk Backus

Reputation: 4866

You will probably get this error if you aren't running the javascript within your extension. The chrome runtime doesn't exist outside that, and if you are running the javascript via injection, the runtime doesn't exist there.

Furthermore, don't forget to include the nativeMessaging permission in the manifest.json.

"permissions": [
    "nativeMessaging"
]

Upvotes: 5

Related Questions