我在将消息从Chrome扩展程序(弹出窗口.html)发送到所选选项卡中注入的脚本时遇到问题。弹出窗口中的代码.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");
在这里我遇到了同样的问题:"味精发送_输出"显示,但"味精发送_输入"没有。如果有人对此问题有任何想法,请告诉我。
如果您查看弹出窗口检查器,您可以看到弹出窗口将出现运行时错误,因此最后一个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");