Path: blob/main/files/en-us/mozilla/add-ons/webextensions/api/cookies/get/index.md
6581 views
------{{AddonSidebar()}}
The get() method of the {{WebExtAPIRef("cookies")}} API retrieves information about a single cookie, given its name and URL.
If more than one cookie with the same name exists for a given URL, the one with the longest path will be returned. For cookies with the same path length, the cookie with the earliest creation time will be returned. If no matching cookie could be found, null is returned.
This is an asynchronous function that returns a Promise.
Syntax
Parameters
details: An
objectcontaining details that can be used to match a cookie to be retrieved. It can include the following properties:firstPartyDomain{{optional_inline}}: A
stringrepresenting the first-party domain with which the cookie to retrieve is associated. This property must be supplied if the browser has first-party isolation enabled. See First-party isolation.
name: A
stringrepresenting the name of the cookie to retrieve.
partitionKey{{optional_inline}}: An
objectrepresenting the storage partition containing the cookie. Include this object withtopLevelSiteto obtain a cookie from partitioned storage. Otherwise, returns the cookie from unpartitioned storage. This object contains:topLevelSite{{optional_inline}}: A
stringrepresenting the first-party URL of the top-level site storage partition containing the cookie.
storeId{{optional_inline}}: A
stringrepresenting the ID of the {{WebExtAPIRef("cookies.CookieStore", "cookie store")}} in which to look for the cookie (as returned by {{WebExtAPIRef("cookies.getAllCookieStores()")}}). By default, the current execution context's cookie store will be used.
url: A
stringrepresenting the URL with which the cookie to retrieve is associated. This argument may be a full URL, in which case any data following the URL path (e.g. the query string) is ignored. If host permissions for this URL are not specified in the extension's manifest file, the API call will fail.
Return value
A Promise that will be fulfilled with a {{WebExtAPIRef('cookies.Cookie', 'Cookie')}} object containing details about the cookie, or null if the cookie was not found.
Browser compatibility
{{Compat}}
Examples
This example tries to get the cookie named "favorite-color", associated with the URL for the currently active tab:
{{WebExtExamples}}
Note: This API is based on Chromium's
chrome.cookiesAPI. This documentation is derived fromcookies.jsonin the Chromium code.