我有一個背景頁面和一個內容腳本。內容腳本有這樣的代碼:從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