Class: Cookies
Class: Cookies
Query and modify a session's cookies.查询和修改会话的cookie。
Process:进程:Main
This class is not exported from the 此类不是从'electron'
module. 'electron'
模块导出的。It is only available as a return value of other methods in the Electron API.它只能作为Electron API中其他方法的返回值使用。
Instances of the Cookies
class are accessed by using cookies
property of a Session
.Cookies
类的实例是通过使用Session
的cookies
属性来访问的。
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
:Cookie
实例:
Event: 'changed'
Returns:返回:
event
Eventcookie
Cookie -The cookie that was changed.被更改的cookie。cause
string - The cause of the change with one of the following values:explicit
-The cookie was changed directly by a consumer's action.cookie是由消费者的行为直接更改的。overwrite
-The cookie was automatically removed due to an insert operation that overwrote it.由于一个覆盖cookie的插入操作,该cookie已被自动删除。expired
-The cookie was automatically removed as it expired.cookie过期后被自动删除。evicted
-The cookie was automatically evicted during garbage collection.cookie在垃圾收集过程中被自动收回。expired-overwrite
-The cookie was overwritten with an already-expired expiration date.cookie被已过期的过期日期覆盖。
removed
boolean -如果cookie被删除,则为true
if the cookie was removed,false
otherwise.true
,否则为false
。
Emitted when a cookie is changed because it was added, edited, removed, or expired.当cookie因添加、编辑、删除或过期而发生更改时发出。
Instance Methods实例方法
The following methods are available on instances of 以下方法可用于Cookies
:Cookies
实例:
cookies.get(filter)
filter
Objecturl
string (optional) -Retrieves cookies which are associated with检索与url
.url
关联的cookie。Empty implies retrieving cookies of all URLs.Empty表示检索所有URL的cookie。name
string (optional) -Filters cookies by name.按名称筛选cookie。domain
string (optional) -Retrieves cookies whose domains match or are subdomains of检索其域匹配或是domains
.domains
的子域的cookie。path
string (optional) -Retrieves cookies whose path matches检索路径与path
.path
匹配的cookie。secure
boolean (optional) -Filters cookies by their Secure property.按Cookie的安全属性筛选Cookie。session
boolean (optional) -Filters out session or persistent cookies.筛选掉会话或持久cookie。
Returns返回Promise<Cookie[]>
- A promise which resolves an array of cookie objects.一个解析cookie对象数组的promise。
Sends a request to get all cookies matching 发送一个请求以获取所有匹配filter
, and resolves a promise with the response.filter
的cookie,并用响应解析一个promise。
cookies.set(details)
details
Objecturl
string -The URL to associate the cookie with.将cookie与之关联的URL。The promise will be rejected if the URL is invalid.如果URL无效,承诺将被拒绝。name
string (optional) -The name of the cookie.cookie的名称。Empty by default if omitted.如果省略,则默认为空。value
string (optional) -The value of the cookie. Empty by default if omitted.cookie的值。如果省略,则默认为空。domain
string (optional) -The domain of the cookie; this will be normalized with a preceding dot so that it's also valid for subdomains.cookie的域;这将用前面的一个点进行规范化,这样它对子域也是有效的。Empty by default if omitted.如果省略,则默认为空。path
string (optional) -The path of the cookie. Empty by default if omitted.cookie的路径。如果省略,则默认为空。secure
boolean (optional) -Whether the cookie should be marked as Secure.cookie是否应标记为安全。Defaults to false unless Same Site=None attribute is used.除非使用Same Site=None属性,否则默认为false
。httpOnly
boolean (optional) -Whether the cookie should be marked as HTTP only. Defaults to false.是否应将cookie标记为仅HTTP。默认为false
。expirationDate
Double (optional) -The expiration date of the cookie as the number of seconds since the UNIX epoch.cookie的过期日期,即自UNIX epoch以来的秒数。If omitted then the cookie becomes a session cookie and will not be retained between sessions.如果省略,则cookie将成为会话cookie,并且不会在会话之间保留。sameSite
string (optional) -The Same Site policy to apply to this cookie.要应用于此cookie的同一站点策略。Can be可以是unspecified
,no_restriction
,lax
orstrict
.unspecified
、no_restriction
、lax
或strict
。Default islax
.
Returns返回Promise<void>
- A promise which resolves when the cookie has been set设置cookie后解决的承诺
Sets a cookie with 设置带有details
.details
的cookie。
cookies.remove(url, name)
url
string -The URL associated with the cookie.与cookie关联的URL。name
string -The name of cookie to remove.要删除的cookie的名称。
Returns返回Promise<void>
- A promise which resolves when the cookie has been removed当cookie被删除时解决的承诺
Removes the cookies matching 删除与url
and name
url
和name
匹配的cookie
cookies.flushStore()
Returns返回Promise<void>
- A promise which resolves when the cookie store has been flushed当cookie存储被刷新时解决的承诺
Writes any unwritten cookies data to disk.将任何未写入的cookie数据写入磁盘。