javascript - Communicate between content script and options page -
i have seen many questions , background page content script.
summary
my extension has options page, , content script. content script handles storage functionality (chrome.storage
manipulation). whenever, user changes setting in options page, want send message content script store new data.
my code:
options.js
var data = "abcd"; // let data chrome.tabs.query({ active: true, currentwindow: true }, function (tabs) { chrome.tabs.sendmessage(tabs[0].id, "storedata:" + data, function(response){ console.log(response); // gives undefined :( }); });
content script js
chrome.runtime.onmessage.addlistener(function(request, sender, sendresponse) { // not working });
my question:
- why isn't approach not working?
- is there other (better) approach procedure.
1) why isn't approach not working?
you trying message yourself: options page on you're doing returned chrome.tabs.query({ active: true, currentwindow: true }, ...);
2) there other (better) approach procedure.
it depends on page you're trying message. message all pages: query empty filter , iterate on result.
you keep track of last active tab content script, sounds overcomplicated.
or, use fact changes chrome.storage
generate onchanged
events in all extension contexts. if modify options saved in chrome.storage
, content scripts can listen onchanged
events , reprocess data needed, , way skip messaging completely.
Comments
Post a Comment