【问题标题】:sending message from extension to tab将消息从扩展程序发送到选项卡
【发布时间】:2012-12-01 21:26:13
【问题描述】:

我在将消息从 Chrome 扩展程序 (popup.html) 发送到在选定选项卡中注入的脚本时遇到问题。 popup.html中的代码如下:

alert("sending MSG");
chrome.tabs.sendMessage(null, {greeting: "hello"}, function(response) {
console.log(response.farewell); 
});
alert("MSG send");

问题是只显示“正在发送消息”警报,但没有显示第二个警报“已发送消息”。这就像它在阻止代码一样。

即使我使用这个功能:

chrome.tabs.getSelected(null, function(tab) {  
chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) { 
console.log(response.farewell); alert("MSG sent _ in");
                                         });
                        });
alert("MSG send _ out");

在这里我遇到了同样的问题:显示了“MSG send _ out”,但没有显示“MSG send _ in”。 如果有人对此问题有任何想法,请告诉我。

【问题讨论】:

    标签: google-chrome google-chrome-extension google-chrome-app


    【解决方案1】:

    如果您查看弹出窗口检查器,您会看到您的弹出窗口将出现运行时错误,因此最后一个 alert 不起作用。

    解决方案很简单, 你应该使用chrome.extension.sendMessage 而不是chrome.tabs.sendMessage

    alert("sending MSG");
    chrome.extension.sendMessage(null, {greeting: "hello"}, function(response) {
        console.log(response.farewell); 
    });
    alert("MSG send");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多