Chrome extensions: storage listener for only one s

2019-08-20 01:52发布

问题:

popup.js:

...
chrome.storage.sync.set({'source': source, 'active': active, 'secs': secs, 'domain': domain}, function() {
    console.log('Settings saved');
});
...

background.js:

chrome.storage.onChanged.addListener(function(tab) {
    //something
});

So, can I add a listener for changes of (for example) 'active'?

回答1:

You'd have to add an if or switch statement in the listener for specific keys, like so;

chrome.storage.onChanged.addListener(function(changes, namespace) {
   for(key in changes) {
     if(key === 'active') {
       // Do something here
     }
   }
 });