storage.onChanged
Fired when storageArea.set, storageArea.remove, or storageArea.clear executes against a storage area.
When this event is triggered by storageArea.set, it's possible to receive a callback when there is no change to the underlying data. Also, the information returned includes all keys within the storage area storageArea.set ran against. The extension can determine the changes that occurred by examining the content of the changes argument received by the onChanged listeners.
Syntax
browser.storage.onChanged.addListener(callback)
browser.storage.onChanged.removeListener(listener)
browser.storage.onChanged.hasListener(listener)
Events have three functions:
-
addListener(callback)
-
Adds a listener to this event.
-
removeListener(listener)
-
Stop listening to this event. The listener argument is the listener to remove.
-
hasListener(listener)
-
Check whether listener is registered for this event. Returns true if it is listening, false otherwise.
Parameters
-
callback
-
The function called when this event occurs. The function is passed these arguments:
-
changes
-
object. Object describing the change. This object contains properties for all the keys in the storage area included in the storageArea.set call, even if key values are unchanged. The name of each property is the name of each key. The value of each key is a storage.StorageChange object describing the change to that item.
-
areaName
-
string. The name of the storage area ("sync", "local", or "managed") to which the changes were made.
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
onChanged |
Yes |
14 |
45 |
? |
Yes |
14 |
? |
? |
48 |
? |
15 |
? |
Examples
function logStorageChange(changes, area) {
console.log(`Change in storage area: ${area}`);
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.onChanged.addListener(logStorageChange);