首页 > 解决方案 > Chrome 扩展本机消息传递语法错误

问题描述

我有一个问题,我的 chrome 扩展在尝试与本机主机应用程序通信时返回此错误。特别是在传递 helo 消息以确保已安装主机应用程序时。


错误:在 chrome-extension://lafoonpfkflnebpoacdbcojcbdhebifh/background.js:35:20 处调用表单 runtime.sendNativeMessage(string, string, function) 与定义 runtime.sendNativeMessage(string application, object message, optional function responseCallback) 不匹配


我尝试传递一个预先存在的对象,一个在 sendNativeMessage 中定义的对象,例如 sendNativeMessage(string,{object:"object"},function) 和一个最近通过新对象定义的对象。

编辑:我也试过 var nativeMessageObject = {};

我觉得我一定是在这里吠错了树什么的。下面是我目前修剪到相关区域的代码。


console.log("Recieved helo host app. Attempting to contact host app.")

var nativeMessageObject = new Object();
nativeMessageObject.data = "helo";
chrome.runtime.sendNativeMessage('appName', nativeMessageObject, function(response) {
  console.log("Host app responded: " + response)
  switch (response) {
    case "ehlo":
      console.log("Host app responded with ehlo")
      sendResponse({
        data: "ehlo host app"
      })
      break;
    default:
      console.log("Error: " + Json.stringify(chrome.runtime.lastError))
      sendResponse({
        data: "Error: " + Json.stringify(chrome.runtime.lastError)
      })
      break;
  }
})

任何援助将不胜感激。

编辑:现在已经尝试过 3 台不同的机器,所有情况下的错误都是相同的。

标签: javascriptgoogle-chrome-extensionchrome-native-messaging

解决方案


推荐阅读