chrome.cookies
Description: |
Use the chrome.cookies API to query and modify cookies, and to be notified when they change.
|
Availability: |
Stable since Chrome 6.
|
Permissions: |
"cookies"
host permissions
|
Manifest
To use the cookies API,
you must declare the "cookies" permission in your manifest,
along with host permissions
for any hosts whose cookies
you want to access.
For example:
manifest.json
{
"name": "My extension",
...
"permissions": [
"cookies",
"*://*.google.com"
],
...
}
Examples
You can find a simple example
of using the cookies API in the
examples/api/cookies
directory.
For other examples
and for help in viewing the source code,
see Samples.
chrome.cookies reference
Types
Cookie
Represents information about an HTTP cookie.
properties of Cookie
-
name
(
string
)
-
The name of the cookie.
-
value
(
string
)
-
The value of the cookie.
-
domain
(
string
)
-
The domain of the cookie (e.g. "www.google.com", "example.com").
-
hostOnly
(
boolean
)
-
True if the cookie is a host-only cookie (i.e. a request's host must exactly match the domain of the cookie).
-
path
(
string
)
-
The path of the cookie.
-
secure
(
boolean
)
-
True if the cookie is marked as Secure (i.e. its scope is limited to secure channels, typically HTTPS).
-
httpOnly
(
boolean
)
-
True if the cookie is marked as HttpOnly (i.e. the cookie is inaccessible to client-side scripts).
-
session
(
boolean
)
-
True if the cookie is a session cookie, as opposed to a persistent cookie with an expiration date.
-
expirationDate
(
optional
double
)
-
The expiration date of the cookie as the number of seconds since the UNIX epoch. Not provided for session cookies.
-
storeId
(
string
)
-
The ID of the cookie store containing this cookie, as provided in getAllCookieStores().
CookieStore
Represents a cookie store in the browser. An incognito mode window, for instance, uses a separate cookie store from a non-incognito window.
-
id
(
string
)
-
The unique identifier for the cookie store.
-
tabIds
(
array of integer
)
-
Identifiers of all the browser tabs that share this cookie store.
Methods
get
chrome.cookies.get(object details, function callback)
Retrieves information about a single cookie. If more than one cookie of the same name exists for the 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.
Parameters
-
details
(
object
)
-
Details to identify the cookie being retrieved.
-
-
url
(
string
)
-
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 simply ignored. If host permissions for this URL are not specified in the manifest file, the API call will fail.
-
name
(
string
)
-
The name of the cookie to retrieve.
-
storeId
(
optional
string
)
-
The ID of the cookie store in which to look for the cookie. By default, the current execution context's cookie store will be used.
Callback
The callback parameter should specify a function
that looks like this:
function(Cookie cookie) {...};
-
cookie
(
optional
Cookie
)
-
Contains details about the cookie. This parameter is null if no such cookie was found.
getAll
chrome.cookies.getAll(object details, function callback)
Retrieves all cookies from a single cookie store that match the given information. The cookies returned will be sorted, with those with the longest path first. If multiple cookies have the same path length, those with the earliest creation time will be first.
Parameters
-
details
(
object
)
-
Information to filter the cookies being retrieved.
-
-
url
(
optional
string
)
-
Restricts the retrieved cookies to those that would match the given URL.
-
name
(
optional
string
)
-
Filters the cookies by name.
-
domain
(
optional
string
)
-
Restricts the retrieved cookies to those whose domains match or are subdomains of this one.
-
path
(
optional
string
)
-
Restricts the retrieved cookies to those whose path exactly matches this string.
-
secure
(
optional
boolean
)
-
Filters the cookies by their Secure property.
-
session
(
optional
boolean
)
-
Filters out session vs. persistent cookies.
-
storeId
(
optional
string
)
-
The cookie store to retrieve cookies from. If omitted, the current execution context's cookie store will be used.
Callback
The callback parameter should specify a function
that looks like this:
function(array of Cookie cookies) {...};
-
cookies
(
array of Cookie
)
-
All the existing, unexpired cookies that match the given cookie info.
set
chrome.cookies.set(object details, function callback)
Sets a cookie with the given cookie data; may overwrite equivalent cookies if they exist.
Parameters
-
details
(
object
)
-
Details about the cookie being set.
-
-
url
(
string
)
-
The request-URI to associate with the setting of the cookie. This value can affect the default domain and path values of the created cookie. If host permissions for this URL are not specified in the manifest file, the API call will fail.
-
name
(
optional
string
)
-
The name of the cookie. Empty by default if omitted.
-
value
(
optional
string
)
-
The value of the cookie. Empty by default if omitted.
-
domain
(
optional
string
)
-
The domain of the cookie. If omitted, the cookie becomes a host-only cookie.
-
path
(
optional
string
)
-
The path of the cookie. Defaults to the path portion of the url parameter.
-
secure
(
optional
boolean
)
-
Whether the cookie should be marked as Secure. Defaults to false.
-
httpOnly
(
optional
boolean
)
-
Whether the cookie should be marked as HttpOnly. Defaults to false.
-
expirationDate
(
optional
double
)
-
The expiration date of the cookie as the number of seconds since the UNIX epoch. If omitted, the cookie becomes a session cookie.
-
storeId
(
optional
string
)
-
The ID of the cookie store in which to set the cookie. By default, the cookie is set in the current execution context's cookie store.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function(Cookie cookie) {...};
-
cookie
(
optional
Cookie
)
-
Contains details about the cookie that's been set. If setting failed for any reason, this will be "null", and "chrome.runtime.lastError" will be set.
remove
chrome.cookies.remove(object details, function callback)
Deletes a cookie by name.
Parameters
-
details
(
object
)
-
Information to identify the cookie to remove.
-
-
url
(
string
)
-
The URL associated with the cookie. If host permissions for this URL are not specified in the manifest file, the API call will fail.
-
name
(
string
)
-
The name of the cookie to remove.
-
storeId
(
optional
string
)
-
The ID of the cookie store to look in for the cookie. If unspecified, the cookie is looked for by default in the current execution context's cookie store.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function(object details) {...};
-
details
(
optional
object
)
-
Contains details about the cookie that's been removed. If removal failed for any reason, this will be "null", and "chrome.runtime.lastError" will be set.
-
-
url
(
string
)
-
The URL associated with the cookie that's been removed.
-
name
(
string
)
-
The name of the cookie that's been removed.
-
storeId
(
string
)
-
The ID of the cookie store from which the cookie was removed.
getAllCookieStores
chrome.cookies.getAllCookieStores(function callback)
Lists all existing cookie stores.
Parameters
Callback
The callback parameter should specify a function
that looks like this:
function(array of CookieStore cookieStores) {...};
-
cookieStores
(
array of CookieStore
)
-
All the existing cookie stores.
Events
onChanged
Fired when a cookie is set or removed. As a special case, note that updating a cookie's properties is implemented as a two step process: the cookie to be updated is first removed entirely, generating a notification with "cause" of "overwrite" . Afterwards, a new cookie is written with the updated values, generating a second notification with "cause" "explicit".
addListener
chrome.cookies.onChanged.addListener(function callback)
Parameters
Callback
The callback parameter should specify a function
that looks like this:
function(object changeInfo) {...};
-
changeInfo
(
object
)
-
-
removed
(
boolean
)
-
True if a cookie was removed.
-
cookie
(
Cookie
)
-
Information about the cookie that was set or removed.