Skip to content Skip to sidebar Skip to footer

Chrome Extensions: Storage Listener For Only One Stored Variable

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

Solution 1:

I know this answer is several years old, but this question showed up in my google searches and the current answer is inefficient, since a direct lookup is possible instead of iteration. This code should work better:

chrome.storage.onChanged.addListener(function(changes, namespace) {
    if ("active"in changes) {
        console.log("Old value: " + changes.active.oldValue);
        console.log("New value: " + changes.active.newValue);
    }
});

Solution 2:

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

Post a Comment for "Chrome Extensions: Storage Listener For Only One Stored Variable"