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:

  1. why isn't approach not working?
  2. 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

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -