Skip to main content

类:Cookies

类:Cookies

🌐 Class: Cookies

查询并修改会话的 Cookie。

进程: 主进程
此类未从 'electron' 模块导出。它仅作为 Electron API 中其他方法的返回值提供。

可以通过 Sessioncookies 属性访问 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 活动
  • cookie Cookie - 被更改的 Cookie。
  • cause 字符串 - 变化的原因,其值为以下之一:
    • explicit - cookie 是由消费者的操作直接更改的。
    • overwrite - 由于插入操作覆盖了它,Cookie 已被自动删除。
    • expired - cookie 过期后会被自动删除。
    • evicted - cookie 在垃圾收集期间被自动逐出。
    • expired-overwrite - 该 Cookie 已被具有已过期失效日期的内容覆盖。
  • removed 布尔值 - 如果 cookie 被移除则为 true,否则为 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 对象
    • url 字符串(可选)- 获取与 url 相关联的 Cookie。为空表示获取所有 URL 的 Cookie。
    • name 字符串(可选)- 按名称筛选 Cookie。
    • domain 字符串(可选)- 检索其域名与 domains 匹配或为其子域的 cookies。
    • path 字符串(可选)- 获取路径与 path 匹配的 cookies。
    • secure 布尔值(可选)- 根据 Cookie 的 Secure 属性进行筛选。
    • session 布尔值(可选)- 过滤掉会话或持久性 cookie。
    • httpOnly 布尔值(可选)- 通过 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 对象
    • url 字符串 - 与该 cookie 关联的 URL。如果 URL 无效,Promise 将被拒绝。
    • name 字符串(可选)- Cookie 的名称。如果省略,默认为空。
    • value 字符串(可选)- Cookie 的值。如果省略,默认为空。
    • domain 字符串(可选)- Cookie 的域;该域会自动加上前导点以使其对子域也有效。如果省略,默认为空。
    • path 字符串(可选)- Cookie 的路径。如果省略,默认为空。
    • secure 布尔值(可选)- 指示 cookie 是否应标记为安全。默认值为 false,除非使用了 Same Site=None 属性。
    • httpOnly 布尔值(可选)- 是否将 Cookie 标记为仅限 HTTP。默认值为 false。
    • expirationDate 双精度(可选)- Cookie 的过期时间,以自 UNIX 纪元起的秒数表示。如果省略,则该 Cookie 成为会话 Cookie,不能在会话之间保留。
    • sameSite 字符串(可选)- 要应用于此 Cookie 的 同站点 策略。可以是 unspecifiedno_restrictionlaxstrict。默认值是 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)

  • url 字符串 - 与该 Cookie 相关联的 URL。
  • name 字符串 - 要删除的 cookie 名称。

返回 Promise<void> - 一个在 cookie 被移除后解决的 Promise

🌐 Returns Promise<void> - A promise which resolves when the cookie has been removed

删除匹配 urlname 的 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.