Chrome extensions: storage listener for only one stored variable

妖精的绣舞 提交于 2019-12-12 04:41:09

问题


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
     }
   }
 });


来源:https://stackoverflow.com/questions/46158592/chrome-extensions-storage-listener-for-only-one-stored-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!