storage.StorageArea.onChanged
Fires when one or more items in a storage area change. Compared to storage.onChanged, this event enables you to listen for changes in one of the storage areas: local, managed, session, or sync.
Note:
In Firefox, the listener receives all the keys from a storage area where storageArea.set executes. The listener may also be invoked when there is no change to the data. To find details of the changed items, examine each key's storage.StorageChange object. See Firefox bug 1833153.
Note:
Firefox does not fire this event for changes to storage.managed because managed storage is only read on browser startup (from the JSON manifest (native manifest) file or 3rdparty enterprise policy).
Syntax
// local can also be sync, managed, or session
browser.storage.local.onChanged.addListener(listener)
browser.storage.local.onChanged.removeListener(listener)
browser.storage.local.onChanged.hasListener(listener)
Events have three functions:
addListener(listener)-
Adds a listener to this event.
removeListener(listener)-
Stops listening to this event. The
listenerargument is the listener to remove. hasListener(listener)-
Checks whether
listeneris registered for this event. Returnstrueif it is listening,falseotherwise.
addListener syntax
>Parameters
listener-
The function called when this event occurs. The function is passed this argument:
changes-
object. Object describing the change. This contains one property for each key that changed. The property name is the name of the key that changed, and its value is astorage.StorageChangeobject describing the change to that item.
Examples
/*
Log the old value and its new value of
changes in the local storage.
*/
function logStorageChange(changes) {
const changedItems = Object.keys(changes);
for (const item of changedItems) {
console.log(`${item} has changed:`);
console.log("Old value: ", changes[item].oldValue);
console.log("New value: ", changes[item].newValue);
}
}
browser.storage.local.onChanged.addListener(logStorageChange);
Browser compatibility
Note:
This API is based on Chromium's chrome.storage API. This documentation is derived from storage.json in the Chromium code.