* feat: promisify the Cookie API * chore: update specs to test promisified cookies * chore: add deprecate wrapper for cookie callback API * docs: update docs to cookie promise changes * chore: remove redundant namespace use * docs: improve cookie example * docs: restore docs for cookie callback API * chore: restore cookie callback tests * fix: syntax of cookie promise return types
6.3 KiB
Class: Cookies
Query and modify a session's cookies.
Process: Main
Instances of the Cookies class are accessed by using cookies property of
a Session.
For example:
const { session } = require('electron')
// Query all cookies.
session.defaultSession.cookies.get({})
.then((cookies) => {
console.log(cookies)
}).catch((error) => {
console.log(error)
})
// 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)
})
// Set a cookie with the given cookie data;
// may overwrite equivalent cookies if they exist.
const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' }
session.defaultSession.cookies.set(cookie)
.then(() => {
// success
}, (error) => {
console.error(error)
})
Instance Events
The following events are available on instances of Cookies:
Event: 'changed'
eventEventcookieCookie - The cookie that was changed.causeString - 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.
removedBoolean -trueif the cookie was removed,falseotherwise.
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)
filterObjecturlString (optional) - Retrieves cookies which are associated withurl. Empty implies retrieving cookies of all urls.nameString (optional) - Filters cookies by name.domainString (optional) - Retrieves cookies whose domains match or are subdomains ofdomains.pathString (optional) - Retrieves cookies whose path matchespath.secureBoolean (optional) - Filters cookies by their Secure property.sessionBoolean (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.
cookies.get(filter, callback)
filterObjecturlString (optional) - Retrieves cookies which are associated withurl. Empty implies retrieving cookies of all urls.nameString (optional) - Filters cookies by name.domainString (optional) - Retrieves cookies whose domains match or are subdomains ofdomains.pathString (optional) - Retrieves cookies whose path matchespath.secureBoolean (optional) - Filters cookies by their Secure property.sessionBoolean (optional) - Filters out session or persistent cookies.
callbackFunctionerrorErrorcookiesCookie[] - an array of cookie objects.
Sends a request to get all cookies matching filter, callback will be called
with callback(error, cookies) on complete.
cookies.set(details)
detailsObjecturlString - The url to associate the cookie with.nameString (optional) - The name of the cookie. Empty by default if omitted.valueString (optional) - The value of the cookie. Empty by default if omitted.domainString (optional) - The domain of the cookie. Empty by default if omitted.pathString (optional) - The path of the cookie. Empty by default if omitted.secureBoolean (optional) - Whether the cookie should be marked as Secure. Defaults to false.httpOnlyBoolean (optional) - Whether the cookie should be marked as HTTP only. Defaults to false.expirationDateDouble (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.
cookies.set(details, callback)
detailsObjecturlString - The url to associate the cookie with.nameString (optional) - The name of the cookie. Empty by default if omitted.valueString (optional) - The value of the cookie. Empty by default if omitted.domainString (optional) - The domain of the cookie. Empty by default if omitted.pathString (optional) - The path of the cookie. Empty by default if omitted.secureBoolean (optional) - Whether the cookie should be marked as Secure. Defaults to false.httpOnlyBoolean (optional) - Whether the cookie should be marked as HTTP only. Defaults to false.expirationDateDouble (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.
callbackFunctionerrorError
Sets a cookie with details, callback will be called with callback(error)
on complete.
cookies.remove(url, name)
urlString - The URL associated with the cookie.nameString - 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
cookies.remove(url, name, callback)
urlString - The URL associated with the cookie.nameString - The name of cookie to remove.callbackFunction
Removes the cookies matching url and name, callback will called with
callback() on complete.
cookies.flushStore()
Returns Promise<void> - A promise which resolves when the cookie store has been flushed
Writes any unwritten cookies data to disk.
cookies.flushStore(callback)
callbackFunction
Writes any unwritten cookies data to disk.