Skip to main content

类: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.

通过使用 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:

事件:'changed'

¥Event: 'changed'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • cookie 曲奇饼 - 被改变的 cookie。

    ¥cookie Cookie - The cookie that was changed.

  • cause 字符串 - 更改原因为以下值之一:

    ¥cause string - 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

    ¥removed boolean - true if the cookie was removed, false otherwise.

当 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 对象

    ¥filter Object

    • url 字符串(可选) - 检索与 url 关联的 cookie。空表示检索所有 URL 的 cookie。

      ¥url string (optional) - Retrieves cookies which are associated with url. Empty implies retrieving cookies of all URLs.

    • name 字符串(可选) - 按名称过滤 cookie。

      ¥name string (optional) - Filters cookies by name.

    • domain 字符串(可选) - 检索域与 domains 匹配或者是 domains 子域的 cookie。

      ¥domain string (optional) - Retrieves cookies whose domains match or are subdomains of domains.

    • path 字符串(可选) - 检索路径与 path 匹配的 cookie。

      ¥path string (optional) - Retrieves cookies whose path matches path.

    • secure 布尔值(可选) - 按安全属性过滤 cookie。

      ¥secure boolean (optional) - Filters cookies by their Secure property.

    • session 布尔值(可选) - 过滤掉会话或持久 cookie。

      ¥session boolean (optional) - Filters out session or persistent cookies.

    • httpOnly 布尔值(可选) - 按 httpOnly 过滤 cookie。

      ¥httpOnly boolean (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 对象

    ¥details Object

    • url 字符串 - 与 cookie 关联的 URL。如果 URL 无效,则 promise 将被拒绝。

      ¥url string - The URL to associate the cookie with. The promise will be rejected if the URL is invalid.

    • name 字符串(可选) - cookie 的名称。如果省略则默认为空。

      ¥name string (optional) - The name of the cookie. Empty by default if omitted.

    • value 字符串(可选) - cookie 的值。如果省略则默认为空。

      ¥value string (optional) - The value of the cookie. Empty by default if omitted.

    • domain 字符串(可选) - 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. Empty by default if omitted.

    • path 字符串(可选) - cookie 的路径。如果省略则默认为空。

      ¥path string (optional) - The path of the cookie. Empty by default if omitted.

    • secure 布尔值(可选) - cookie 是否应标记为安全。除非使用 同一站点=无 属性,否则默认为 false。

      ¥secure boolean (optional) - Whether the cookie should be marked as Secure. Defaults to false unless Same Site=None attribute is used.

    • httpOnly 布尔值(可选) - cookie 是否应标记为仅 HTTP。默认为 false。

      ¥httpOnly boolean (optional) - Whether the cookie should be marked as HTTP only. Defaults to false.

    • expirationDate 双(可选) - cookie 的过期日期为自 UNIX 纪元以来的秒数。如果省略,则 cookie 将成为会话 cookie,并且不会在会话之间保留。

      ¥expirationDate Double (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 的 同一站点 策略。可以是 unspecifiedno_restrictionlaxstrict。默认为 lax

      ¥sameSite string (optional) - The Same Site policy to apply to this cookie. Can be unspecified, no_restriction, lax or strict. Default is 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。

    ¥url string - The URL associated with the cookie.

  • name 字符串 - 要删除的 cookie 的名称。

    ¥name string - The name of cookie to remove.

返回 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.