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

Chr*_*_vr 1 google-api google-apps google-chrome-extension google-chrome-app chrome-native-messaging

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

1.C++ conole app 2.JS文件3. manifet文件我已经创建了这样的注册表项 在此输入图像描述

现在我在行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();
}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

Kir*_*kus 5

如果您没有在扩展程序中运行javascript,则可能会收到此错误.Chrome运行时不存在于此之外,如果您通过注入运行javascript,则运行时不存在.

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

"permissions": [
    "nativeMessaging"
]
Run Code Online (Sandbox Code Playgroud)