类:Cookies
类:Cookies
¥Class: Cookies
查询和修改会话的 cookie。
¥Query and modify a session's cookies.
进程:主进程
该类不是从 'electron' 模块导出的。它仅可用作 Electron API 中其他方法的返回值。
¥Process: Main
This class is not exported from the 'electron' module. It is only available as a return value of other methods in the Electron API.
通过使用 Session 的 cookies 属性来访问 Cookies 类的实例。
¥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: 'https://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: 'https://www.github.com', name: 'dummy_name', value: 'dummy' }
session.defaultSession.cookies.set(cookie)
.then(() => {
// success
}, (error) => {
console.error(error)
})
实例事件
¥Instance Events
以下事件在 Cookies 的实例上可用:
¥The following events are available on instances of Cookies:
事件:'changed'
¥Event: 'changed'
返回:
¥Returns:
-
event事件¥
eventEvent -
cookie曲奇饼 - 被改变的 cookie。¥
cookieCookie - The cookie that was changed. -
cause字符串 - 更改原因为以下值之一:¥
causestring - The cause of the change with one of the following values:-
explicit- cookie 是由消费者的操作直接更改的。¥
explicit- The cookie was changed directly by a consumer's action. -
overwrite- 由于插入操作覆盖了 cookie,该 cookie 被自动删除。¥
overwrite- The cookie was automatically removed due to an insert operation that overwrote it. -
expired- cookie 过期后会被自动删除。¥
expired- The cookie was automatically removed as it expired. -
evicted- cookie 在垃圾收集期间被自动逐出。¥
evicted- The cookie was automatically evicted during garbage collection. -
expired-overwrite- cookie 被已过期的过期日期覆盖。¥
expired-overwrite- The cookie was overwritten with an already-expired expiration date.
-
-
removed布尔值 - 如果 cookie 被删除,则为true,否则为false。¥
removedboolean -trueif the cookie was removed,falseotherwise.
当 cookie 由于添加、编辑、删除或过期而发生更改时发出。
¥Emitted when a cookie is changed because it was added, edited, removed, or expired.
实例方法
¥Instance Methods
以下方法可用于 Cookies 的实例:
¥The following methods are available on instances of Cookies:
cookies.get(filter)
-
filter对象¥
filterObject-
url字符串(可选) - 检索与url关联的 cookie。空表示检索所有 URL 的 cookie。¥
urlstring (optional) - Retrieves cookies which are associated withurl. Empty implies retrieving cookies of all URLs. -
name字符串(可选) - 按名称过滤 cookie。¥
namestring (optional) - Filters cookies by name. -
domain字符串(可选) - 检索域与domains匹配或者是domains子域的 cookie。¥
domainstring (optional) - Retrieves cookies whose domains match or are subdomains ofdomains. -
path字符串(可选) - 检索路径与path匹配的 cookie。¥
pathstring (optional) - Retrieves cookies whose path matchespath. -
secure布尔值(可选) - 按安全属性过滤 cookie。¥
secureboolean (optional) - Filters cookies by their Secure property. -
session布尔值(可选) - 过滤掉会话或持久 cookie。¥
sessionboolean (optional) - Filters out session or persistent cookies. -
httpOnly布尔值(可选) - 按 httpOnly 过滤 cookie。¥
httpOnlyboolean (optional) - Filters cookies by httpOnly.
-
返回 Promise<Cookie[]> - 解析 cookie 对象数组的 promise。
¥Returns Promise<Cookie[]> - A promise which resolves an array of cookie objects.
发送请求以获取与 filter 匹配的所有 cookie,并通过响应解析 promise。
¥Sends a request to get all cookies matching filter, and resolves a promise with
the response.
cookies.set(details)
-
details对象¥
detailsObject-
url字符串 - 与 cookie 关联的 URL。如果 URL 无效,则 promise 将被拒绝。¥
urlstring - The URL to associate the cookie with. The promise will be rejected if the URL is invalid. -
name字符串(可选) - cookie 的名称。如果省略则默认为空。¥
namestring (optional) - The name of the cookie. Empty by default if omitted. -
value字符串(可选) - cookie 的值。如果省略则默认为空。¥
valuestring (optional) - The value of the cookie. Empty by default if omitted. -
domain字符串(可选) - cookie 的域;这将使用前面的点进行标准化,以便它对于子域也有效。如果省略则默认为空。¥
domainstring (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字符串(可选) - cookie 的路径。如果省略则默认为空。¥
pathstring (optional) - The path of the cookie. Empty by default if omitted. -
secure布尔值(可选) - cookie 是否应标记为安全。除非使用 同一站点=无 属性,否则默认为 false。¥
secureboolean (optional) - Whether the cookie should be marked as Secure. Defaults to false unless Same Site=None attribute is used. -
httpOnly布尔值(可选) - cookie 是否应标记为仅 HTTP。默认为 false。¥
httpOnlyboolean (optional) - Whether the cookie should be marked as HTTP only. Defaults to false. -
expirationDate双(可选) - cookie 的过期日期为自 UNIX 纪元以来的秒数。如果省略,则 cookie 将成为会话 cookie,并且不会在会话之间保留。¥
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. -
sameSite字符串(可选) - 应用于此 cookie 的 同一站点 策略。可以是unspecified、no_restriction、lax或strict。默认为lax。¥
sameSitestring (optional) - The Same Site policy to apply to this cookie. Can beunspecified,no_restriction,laxorstrict. Default islax.
-
返回 Promise<void> - 设置 cookie 后解决的 promise
¥Returns Promise<void> - A promise which resolves when the cookie has been set
使用 details 设置 cookie。
¥Sets a cookie with details.
cookies.remove(url, name)
-
url字符串 - 与 cookie 关联的 URL。¥
urlstring - The URL associated with the cookie. -
name字符串 - 要删除的 cookie 的名称。¥
namestring - The name of cookie to remove.
返回 Promise<void> - 当 cookie 被删除时解决的 promise
¥Returns Promise<void> - A promise which resolves when the cookie has been removed
删除匹配 url 和 name 的 cookie
¥Removes the cookies matching url and name
cookies.flushStore()
返回 Promise<void> - 当 cookie 存储被刷新时解决的 promise
¥Returns Promise<void> - A promise which resolves when the cookie store has been flushed
将所有未写入的 cookie 数据写入磁盘
¥Writes any unwritten cookies data to disk
任何方法写入的 cookie 都不会立即写入磁盘,而是每 30 秒或 512 次操作写入一次
¥Cookies written by any method will not be written to disk immediately, but will be written every 30 seconds or 512 operations
调用此方法可以使 cookie 立即写入磁盘。
¥Calling this method can cause the cookie to be written to disk immediately.