问题
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