我有一个背景页面和一个内容脚本。内容脚本有这样的代码:从background.js发送消息到内容脚本
chrome.runtime.sendMessage({ getSetting: { setting: "hideAuth" } }, function (hide) {
// this should be executed after 'respond(setting)' in the backgroud page
});
背景页有这样的代码:
chrome.runtime.onMessage.addListener(function (msg, sender, respond) {
if (msg["getSetting"]) {
chrome.storage.sync.get(msg.getSetting.setting, function (setting) {
respond(setting); // this should callback to the content script
});
});
}
});
但是,永远不会执行的内容脚本的响应回调。我尝试过查询当前标签并手动发送消息,但标签仍然没有收到消息。有没有人处理过这个?是否有另一种方法来做到这一点?
http://stackoverflow.com/a/20077854 – rsanchez