Path: blob/main/files/en-us/mozilla/add-ons/webextensions/api/cookies/onchanged/index.md
6580 views
------{{AddonSidebar()}}
The onChanged event of the {{WebExtAPIRef("cookies")}} API fires when a cookie that the extension can access is set or removed.
Note: When storage partitioning is active,
cookies.Cookie.partitionKeycontains the description of the cookie's storage partition. When modifying cookies, it's important to pass this value to {{WebExtAPIRef("cookies.set()")}} or {{WebExtAPIRef("cookies.remove()")}} to ensure the extension works with the correct cookie.
Note that updating a cookie's properties is implemented as a two step process:
First, the cookie to be updated is first removed entirely, generating a notification with a {{WebExtAPIRef("cookies.OnChangedCause")}} of
overwrite.Next, a new cookie is written with the updated values, generating a second notification with a {{WebExtAPIRef("cookies.OnChangedCause")}} of
explicit.
Syntax
This API is also available as browser.cookies.onChanged.*.
Events have three functions:
addListener(callback): Adds a listener to this event.
removeListener(listener): Stop listening to this event. The
listenerargument is the listener to remove.
hasListener(listener): Check whether
listeneris registered for this event. Returnstrueif it is listening,falseotherwise.
addListener syntax
Parameters
callback: A callback function that will be called when this event occurs. The function will be passed the following arguments:
changeInfo: An
objectcontaining details of the change that occurred. Its properties are as follows:removed: A
booleanthat is set totrueif a cookie was removed, and false if not.
cookie: A {{WebExtAPIRef('cookies.Cookie')}} object containing information about the cookie that was set or removed.
cause: A {{WebExtAPIRef('cookies.OnChangedCause')}} value representing the underlying reason behind the cookie's change.
Browser compatibility
{{Compat}}
Examples
This example listens for onChanged events and logs details from the changeInfo argument:
{{WebExtExamples}}
Note: This API is based on Chromium's
chrome.cookiesAPI. This documentation is derived fromcookies.jsonin the Chromium code.