google-chrome - chrome.runtime.connectNative 生成​​未捕获的类型错误 : undefined is not a function

标签 google-chrome google-chrome-extension chrome-native-messaging

我确实编写了一个 chrome 扩展,它调用这个 connect() 函数来连接到本地 C++ 程序:

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

我可以在控制台中看到 test1,但后来我得到了错误
Uncaught TypeError: undefined is not a function

在行中
port = chrome.runtime.connectNative('com.a.chrome_interface');

我的扩展 list 文件在这里:
{
  "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

}

我的 com.a.chrome_interface.json 看起来像这样:
{
"name": "com.a.chrome_interface",
"description": "Chrome Native Messaging API Example Host",
"path": "com.a.chrome_interface",
"type": "stdio",
"allowed_origins": [
"chrome-extension://abc.../"
]
}

和 com.a.chrome_interface 是一个 linux 可执行 C++ 文件,它会生成一个文件,如果它被调用并且这个文件永远不会被创建。
我确实把两个文件都放进去了
 /etc/opt/chrome/native-messaging-hosts/

所以我猜,我确实正确地注册了我的 C++,但我也猜想,如果我注册错了,我应该得到一个不同的错误。
如果我使用 chrome.extension.connect() 脚本运行低谷并且错误消息消失但没有数据到达我的 C++ 程序。

我确实阅读并尝试按照说明进行操作
https://developer.chrome.com/extensions/messaging#native-messaging
并用谷歌搜索了很多,但我可以找出问题的原因。

我在 Ubuntu 12.04 上使用 Chromium 34。
  • 在编写扩展程序时,是否必须使用 chrome.runtime.connectNative() 或 chrome.extension.connectNative()?
  • 如何连接数据并将数据发送到我的 C++ 程序?
  • 最佳答案

    connectNative()在内容脚本中不可用。
    要连接到本地程序,内容脚本必须发送数据,例如到扩展程序的后台脚本和后台脚本中,port = chrome.extension.connectNative可以使用。
    所以这里有一个解决方案:

    内容脚本.js:

    ....
    // send data to background script
    chrome.extension.sendRequest("Some Data");
    ....
    

    背景.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 如上所述在我的问题中,但另外:
    ...
      "background": {
      "scripts": ["background.js"]
      },
    ...
    
    com.a.chrome_interface.json与上面的问题一样不变。

    关于google-chrome - chrome.runtime.connectNative 生成​​未捕获的类型错误 : undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24726026/

    相关文章:

    css - 媒体查询适用于 chrome 但不适用于我的移动设备

    javascript - 有没有办法通过网页/JavaScript 检查是否安装了谷歌浏览器(和默认浏览器),而无需在客户端安装任何东西?

    c++ - 使用 C++ 插件从 chrome 浏览器下载并运行 exe

    python - 您正在使用不受支持的命令行标志 : --ignore-certificate-errors. 稳定性和安全性将受到影响

    javascript - Chrome 扩展程序 - 进入全屏模式会将页面背景颜色变为黑色

    javascript - Chrome 运行时 OnMessage 监听器事件未触发

    javascript - 如何使用 javascript 获取下一个/转发 url?

    google-chrome - 从 native 主机向浏览器扩展发送消息时获取 "Error when communicating with the native messaging host."(Windows)

    c++ - 如何从 Chrome 扩展程序向 native 应用程序发送消息?

    javascript - &lt;input type="date> onchange 触发太多