如何在没有responseCallback的情况下使用chrome.runtime.sendMessage?

时间:2018-07-11 01:43:14

标签: javascript google-chrome-extension

chrome.runtime's page中所指定,responseCallback是sendMessage的可选参数。但是,这种方式似乎并不可行,因为当我编写如下的sendMessage代码时:

chrome.runtime.sendMessage("hello")

单击添加sendMessage作为事件处理程序的按钮时,出现此错误:

  

未捕获的错误:调用runtime.connect(null,)形式与定义runtime.connect(可选字符串extensionId,可选对象connectInfo)不匹配

(注意:runtime.connect不是我的代码的一部分。我猜sendMessage会调用它。)

但是当我这样编码sendMessage时:

chrome.runtime.sendMessage("hello", function(response) {});

没有错误,一切正常。我的分机收到消息。这是我的扩展程序中的代码段:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    console.log("from a content script: " + sender.tab.url);
    console.log("message: " + message);
});

这是控制台中显示的输出:

  

来自内容脚本:http://localhost:8000/practice%20page/index.html

     

消息:你好

我很好(有点),可以使用一个空函数作为第二个参数来调用sendMessage以使其正常工作,但是看起来太不必要了而且很不干净。

我在这里错过了什么吗? chrome的扩展API是否进行了更新,使responseCallback成为sendMessage的必需参数? (也许developer.chrome.com/extensions/runtime page只是没有更新以反映该更改?)

半路!

0 个答案:

没有答案