未捕获的TypeError:无法在Native Messaging App中调用方法'connectNative'

时间:2013-10-21 19:30:31

标签: google-chrome-extension google-api google-apps google-chrome-app chrome-native-messaging

我正在使用本机消息传递应用。我创建了以下文件

1.C ++ conole app 2.JS文件 3.明确档案 我创建了像enter image description here

这样的注册表项

现在我收到错误 port = chrome.runtime.connectNative(hostName); 。我注意到chrome.runtime本身是undefined.let我知道我在这里遗漏了什么。

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();
}

1 个答案:

答案 0 :(得分:5)

如果您未在扩展程序中运行javascript,则可能会收到此错误。 chrome运行时不存在于此之外,如果您通过注入运行javascript,那么运行时就不存在。

此外,不要忘记在manifest.json中包含 nativeMessaging 权限。

"permissions": [
    "nativeMessaging"
]