使用chrome.runtime.sendMessage()将消息发送到弹出窗口时无响应

时间:2019-06-29 23:24:10

标签: javascript google-chrome-extension

我正在构建一个包含contextMenu和一个弹出窗口的Chrome扩展程序。每当我右键单击文本并单击应用程序的ContextMenu时,都应该弹出一个单独的窗口,其中包含一些信息。

eventPage.js的某些部分看起来像这样:

return new Promise((reslove, reject) => {
    fetch(url, myInit)
        .then(response => response.json())
        .then(responseText => {
            var popup_url = 'popup.html'
            var createData = {
                "url": popup_url,
                "type": "popup",
                "top": 5,
                "left": 5,
                "width": 500,
                "height": 500
            };
            chrome.windows.create(createData, function () { });

            chrome.runtime.sendMessage({
                msg: "something_completed",
                data: {
                    subject: "Loading",
                    content: "Just completed!"
                }
            });
            console.log(responseText)
        }).catch(err => {
            //console.log(err);
            reject(err);
        });
}).catch(err => {
    console.log(err);
});

当选择contextMenu并发送消息时,我呼叫popup.html

msg: "something_completed",
data: {
    subject: "Loading",
    content: "Just completed!"
}

使用runtime.sendMessage。在popup.js中包含的我的popup.html中,它具有接收器

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.msg === "something_completed") {
            //  To do something
            alert(request.msg)
            console.log(request.data.subject)
            console.log(request.data.content)
        }
    }
);

但是,什么也没发生。我遵循了文档,但似乎出了点问题。有什么帮助吗?

0 个答案:

没有答案