electron/docs/api/cookies.md

180 lines
6.5 KiB
Markdown
Raw Normal View History

2016-11-10 20:25:26 +00:00
## Class: Cookies
> Query and modify a session's cookies.
2016-11-23 19:20:56 +00:00
Process: [Main](../glossary.md#main-process)
2016-11-10 20:25:26 +00:00
Instances of the `Cookies` class are accessed by using `cookies` property of
a `Session`.
For example:
```javascript
2018-09-13 16:10:51 +00:00
const { session } = require('electron')
2016-11-10 20:25:26 +00:00
// Query all cookies.
session.defaultSession.cookies.get({})
.then((cookies) => {
console.log(cookies)
}).catch((error) => {
console.log(error)
})
2016-11-10 20:25:26 +00:00
// Query all cookies associated with a specific url.
session.defaultSession.cookies.get({ url: 'http://www.github.com' })
.then((cookies) => {
console.log(cookies)
}).catch((error) => {
console.log(error)
})
2016-11-10 20:25:26 +00:00
// Set a cookie with the given cookie data;
// may overwrite equivalent cookies if they exist.
2018-09-13 16:10:51 +00:00
const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' }
session.defaultSession.cookies.set(cookie)
.then(() => {
// success
}, (error) => {
console.error(error)
})
2016-11-10 20:25:26 +00:00
```
### Instance Events
The following events are available on instances of `Cookies`:
#### Event: 'changed'
* `event` Event
* `cookie` [Cookie](structures/cookie.md) - The cookie that was changed.
2016-11-10 20:25:26 +00:00
* `cause` String - The cause of the change with one of the following values:
* `explicit` - The cookie was changed directly by a consumer's action.
* `overwrite` - The cookie was automatically removed due to an insert
operation that overwrote it.
* `expired` - The cookie was automatically removed as it expired.
* `evicted` - The cookie was automatically evicted during garbage collection.
* `expired-overwrite` - The cookie was overwritten with an already-expired
expiration date.
* `removed` Boolean - `true` if the cookie was removed, `false` otherwise.
Emitted when a cookie is changed because it was added, edited, removed, or
expired.
### Instance Methods
The following methods are available on instances of `Cookies`:
#### `cookies.get(filter)`
* `filter` Object
* `url` String (optional) - Retrieves cookies which are associated with
`url`. Empty implies retrieving cookies of all urls.
* `name` String (optional) - Filters cookies by name.
* `domain` String (optional) - Retrieves cookies whose domains match or are
subdomains of `domains`.
* `path` String (optional) - Retrieves cookies whose path matches `path`.
* `secure` Boolean (optional) - Filters cookies by their Secure property.
* `session` Boolean (optional) - Filters out session or persistent cookies.
Returns `Promise<Cookie[]>` - A promise which resolves an array of cookie objects.
Sends a request to get all cookies matching `filter`, and resolves a promise with
the response.
2016-11-10 20:25:26 +00:00
#### `cookies.get(filter, callback)`
* `filter` Object
* `url` String (optional) - Retrieves cookies which are associated with
`url`. Empty implies retrieving cookies of all urls.
* `name` String (optional) - Filters cookies by name.
* `domain` String (optional) - Retrieves cookies whose domains match or are
subdomains of `domains`.
2016-11-10 20:25:26 +00:00
* `path` String (optional) - Retrieves cookies whose path matches `path`.
* `secure` Boolean (optional) - Filters cookies by their Secure property.
* `session` Boolean (optional) - Filters out session or persistent cookies.
* `callback` Function
* `error` Error
2017-06-08 17:09:40 +00:00
* `cookies` [Cookie[]](structures/cookie.md) - an array of cookie objects.
2016-11-10 20:25:26 +00:00
2017-12-02 00:27:53 +00:00
Sends a request to get all cookies matching `filter`, `callback` will be called
2016-11-10 20:25:26 +00:00
with `callback(error, cookies)` on complete.
**[Deprecated Soon](modernization/promisification.md)**
#### `cookies.set(details)`
* `details` Object
* `url` String - The url to associate the cookie with.
* `name` String (optional) - The name of the cookie. Empty by default if omitted.
* `value` String (optional) - The value of the cookie. Empty by default if omitted.
* `domain` String (optional) - The domain of the cookie; this will be normalized with a preceding dot so that it's also valid for subdomains. Empty by default if omitted.
* `path` String (optional) - The path of the cookie. Empty by default if omitted.
* `secure` Boolean (optional) - Whether the cookie should be marked as Secure. Defaults to
false.
* `httpOnly` Boolean (optional) - Whether the cookie should be marked as HTTP only.
Defaults to false.
* `expirationDate` Double (optional) - The expiration date of the cookie as the number of
seconds since the UNIX epoch. If omitted then the cookie becomes a session
cookie and will not be retained between sessions.
Returns `Promise<void>` - A promise which resolves when the cookie has been set
Sets a cookie with `details`.
2016-11-10 20:25:26 +00:00
#### `cookies.set(details, callback)`
* `details` Object
* `url` String - The url to associate the cookie with.
2016-12-29 22:11:26 +00:00
* `name` String (optional) - The name of the cookie. Empty by default if omitted.
* `value` String (optional) - The value of the cookie. Empty by default if omitted.
* `domain` String (optional) - The domain of the cookie. Empty by default if omitted.
* `path` String (optional) - The path of the cookie. Empty by default if omitted.
* `secure` Boolean (optional) - Whether the cookie should be marked as Secure. Defaults to
2016-11-10 20:25:26 +00:00
false.
2016-12-29 22:11:26 +00:00
* `httpOnly` Boolean (optional) - Whether the cookie should be marked as HTTP only.
2016-11-10 20:25:26 +00:00
Defaults to false.
2016-12-29 22:11:26 +00:00
* `expirationDate` Double (optional) - The expiration date of the cookie as the number of
2016-11-10 20:25:26 +00:00
seconds since the UNIX epoch. If omitted then the cookie becomes a session
cookie and will not be retained between sessions.
* `callback` Function
* `error` Error
Sets a cookie with `details`, `callback` will be called with `callback(error)`
on complete.
**[Deprecated Soon](modernization/promisification.md)**
#### `cookies.remove(url, name)`
* `url` String - The URL associated with the cookie.
* `name` String - The name of cookie to remove.
Returns `Promise<void>` - A promise which resolves when the cookie has been removed
Removes the cookies matching `url` and `name`
2016-11-10 20:25:26 +00:00
#### `cookies.remove(url, name, callback)`
* `url` String - The URL associated with the cookie.
* `name` String - The name of cookie to remove.
* `callback` Function
Removes the cookies matching `url` and `name`, `callback` will called with
`callback()` on complete.
2017-04-14 13:12:22 +00:00
**[Deprecated Soon](modernization/promisification.md)**
#### `cookies.flushStore()`
Returns `Promise<void>` - A promise which resolves when the cookie store has been flushed
Writes any unwritten cookies data to disk.
2017-04-14 17:03:52 +00:00
#### `cookies.flushStore(callback)`
* `callback` Function
2017-04-14 13:12:22 +00:00
Writes any unwritten cookies data to disk.
**[Deprecated Soon](modernization/promisification.md)**