Chrome事件监听器SendResponse即使我返回true,也不会发送

时间:2019-11-30 05:34:29

标签: javascript google-chrome-extension

遇到与Chrome Extension Message passing: response not sent相同的问题。但是,当我实施解决方案建议时,并不能解决我的问题。我得到了完全相同的错误,并且在返回响应之前也调用了异步方法。这里出了什么问题???

background.js

chrome.runtime.onMessage.addListener(
    async function (request, sender, sendResponse) {
        if (request.action === "getAllTabs") {
            // if I uncomment this line, content.js receives this response
            // sendResponse({data: "hey"})
            chrome.tabs.query({}, function(tabs){
                // but not this one??
                sendResponse({data: tabs});
            })
            console.log('returning true');
            return true;

我的content.js:

    chrome.runtime.sendMessage({action: "getAllTabs"}, function(response) {
      console.log(response);
    });

0 个答案:

没有答案