类:Cookies
类:Cookies
🌐 Class: Cookies
查询并修改会话的 Cookie。
进程: 主进程
此类未从 'electron' 模块导出。它仅作为 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:
Event: 'changed'
返回:
🌐 Returns:
event事件cookieCookie - 被更改的 cookie。causestring - 变化原因的取值如下之一:inserted- cookie 已被插入。inserted-no-change-overwrite- 新插入的 Cookie 覆盖了一个 Cookie,但没有造成任何变化。例如,插入一个相同的 Cookie 会导致这种情况。inserted-no-value-change-overwrite- 新插入的 Cookie 覆盖了一个 Cookie,但没有导致任何值的变化,但它是网页可观察的(例如更新了过期时间)。explicit- 该 Cookie 是由消费者的操作直接删除的。overwrite- 由于插入操作覆盖了它,Cookie 已被自动删除。expired- cookie 过期后会被自动删除。evicted- cookie 在垃圾收集期间被自动逐出。expired-overwrite- 该 Cookie 已被具有已过期失效日期的内容覆盖。
removedboolean -true如果 cookie 被移除,否则false。
当 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对象urlstring(optional)- 检索与url关联的 cookie。为空表示检索所有 URL 的 cookie。namestring(optional)- 按名称过滤 cookies。domainstring(optional)- 检索其域名匹配或为domains子域的 Cookie。pathstring(optional)- 检索路径与path匹配的 Cookie。secureboolean(optional)- 按其 Secure 属性过滤 cookies。sessionboolean(optional)- 过滤会话或持久性 Cookie。httpOnlyboolean(optional)- 按 httpOnly 过滤 cookies。
返回 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对象urlstring - 与 cookie 关联的 URL。如果 URL 无效,该承诺将被拒绝。namestring(optional)- Cookie 的名称。如果省略,默认为空。valuestring(optional)- Cookie 的值。如果省略,默认为空。domainstring(optional)- Cookie 的域;这将通过前置点进行规范化,以使其对子域也有效。如果省略,默认为空。pathstring(optional)- Cookie 的路径。若省略,默认为空。secureboolean(optional)- 是否应将 Cookie 标记为安全。默认为 false,除非使用了 Same Site=None 属性。httpOnlyboolean(optional)- 是否应将 cookie 标记为仅限 HTTP。默认值为 false。expirationDateDouble(optional)- cookie 的过期时间,以自 UNIX 纪元以来的秒数表示。如果省略,则该 cookie 将成为会话 cookie,并且在会话之间不会被保留。sameSitestring(optional)- 此 Cookie 将应用 Same Site 策略。可以是unspecified、no_restriction、lax或strict。默认是lax。
返回 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)
urlstring - 与该 cookie 关联的 URL。namestring - 要删除的 cookie 名称。
返回 Promise<void> - 当 cookie 被移除时会被解决的承诺。
🌐 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.