Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mohamedkhallouq
GitHub Repository: mohamedkhallouq/content
Path: blob/main/files/en-us/mozilla/add-ons/webextensions/api/cookies/onchanged/index.md
6580 views
---
title: cookies.onChanged slug: Mozilla/Add-ons/WebExtensions/API/cookies/onChanged page-type: webextension-api-event tags: - API - Add-ons - Cookies - Event - Extensions - Non-standard - Reference - WebExtensions - onChanged browser-compat: webextensions.api.cookies.onChanged
---

{{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.partitionKey contains 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:

  1. First, the cookie to be updated is first removed entirely, generating a notification with a {{WebExtAPIRef("cookies.OnChangedCause")}} of overwrite.

  2. Next, a new cookie is written with the updated values, generating a second notification with a {{WebExtAPIRef("cookies.OnChangedCause")}} of explicit.

Syntax

browser.cookies.onChanged.addListener(listener) browser.cookies.onChanged.removeListener(listener) browser.cookies.onChanged.hasListener(listener)

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 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.

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 object containing details of the change that occurred. Its properties are as follows:

          • removed

            • : A boolean that is set to true if 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:

browser.cookies.onChanged.addListener((changeInfo) => { console.log(`Cookie changed: \n` + ` * Cookie: ${JSON.stringify(changeInfo.cookie)}\n` + ` * Cause: ${changeInfo.cause}\n` + ` * Removed: ${changeInfo.removed}`); });

{{WebExtExamples}}

Note: This API is based on Chromium's chrome.cookies API. This documentation is derived from cookies.json in the Chromium code.