Class: Cookies
Class: Cookies
Abfragen und modifizieren von Session Cookies.
Process: Main
This class is not exported from the 'electron' module. Es ist nur als Rückgabewert anderer Methoden in der Electron-API verfügbar.
Auf Instanzen der Cookies-Klasse wird über die Cookie-Eigenschaft einer Sitzung zugegriffen.
Ein Beispiel:
const { session } = require('electron')
// Durchsuche all Cookies.
session.defaultSession.cookies.get({})
.then((cookies) => {
console.log(cookies)
}).catch((error) => {
console.log(error)
})
// Alle Cookies abfragen, die mit einer bestimmten URL verknüpft sind.
session.defaultSession.cookies.get({ url: 'https://www.github.com' })
.then((cookies) => {
console.log(cookies)
}).catch((error) => {
console.log(error)
})
// Setzt einen Cookie mit den gegebenen Cookie-Daten;
// kann äquivalente Cookies überschreiben, wenn diese existieren.
const cookie = { url: 'https://www.github.com', name: 'dummy_name', value: 'dummy' }
session.defaultSession.cookies.set(cookie)
.then(() => {
// Erfolg
}, (error) => {
console.error(error)
})
Instanz-Ereignisse
Folgende Ereignisse sind für Instanzen von Cookies verfügbar:
Event: 'changed'
Kehrt zurück:
eventEventcookieCookie - The cookie that was changed.causestring - Die Ursache für die Änderung mit einem der folgenden Werte:explicit- Der Cookie wurde direkt durch die Aktion eines Verbrauchers geändert.overwrite- Der Cookie wurde automatisch entfernt, da er durch eine Einfügeoperation mit überschrieben wurde.expired- Der Cookie wurde automatisch gelöscht, da er abgelaufen ist.evicted- The cookie was automatically evicted during garbage collection.expired-overwrite- The cookie was overwritten with an already-expired expiration date.
removedboolean -trueif the cookie was removed,falseotherwise.
Emitted when a cookie is changed because it was added, edited, removed, or expired.
Beispiel Methoden
Die folgenden Methoden sind verfügbar in Instanzen von Cookies:
cookies.get(filter)
filterObjekturlstring (optional) - Retrieves cookies which are associated withurl. Empty implies retrieving cookies of all URLs.namestring (optional) - Filters cookies by name.domainstring (optional) - Retrieves cookies whose domains match or are subdomains ofdomains.pathstring (optional) - Retrieves cookies whose path matchespath.secureboolean (optional) - Filters cookies by their Secure property.sessionboolean (optional) - Filters out session or persistent cookies.httpOnlyboolean (optional) - Filters cookies by httpOnly.
Returns Promise<Cookie[]> - A promise which resolves an array of cookie objects.
Sends a request to get all cookies matching filter, and resolves a promise with the response.
cookies.set(details)
detailsObjekturlstring - The URL to associate the cookie with. The promise will be rejected if the URL is invalid.namestring (optional) - The name of the cookie. Empty by default if omitted.valuestring (optional) - Der Wert des Cookies. Empty by default if omitted.domainstring (optional) - Die Domain des Cookie; dies wird mit einem vorhergehenden Punkt normalisiert, so dass es auch für Subdomains gilt. Empty by default if omitted.pathstring (optional) - Der Pfad des Cookie. Empty by default if omitted.secureboolean (optional) - Whether the cookie should be marked as Secure. Defaults to false unless Same Site=None attribute is used.httpOnlyboolean (optional) - Whether the cookie should be marked as HTTP only. Der Standardwert ist false.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.sameSitestring (optional) - The Same Site policy to apply to this cookie. Sein Wert kannnicht gesetzt,no_restriction,laxoderstrictsein. Standard istlax.
Returns Promise<void> - A promise which resolves when the cookie has been set
Legt ein Cookie mit details fest.
cookies.remove(url, name)
urlstring - Die URL, die dem Cookie zugeordnet ist.namestring - Der Name des zu löschenden Cookies.
Returns Promise<void> - A promise which resolves when the cookie has been removed
Removes the cookies matching url and name
cookies.flushStore()
Returns Promise<void> - A promise which resolves when the cookie store has been flushed
Writes any unwritten cookies data to disk
Cookies written by any method will not be written to disk immediately, but will be written every 30 seconds or 512 operations
Calling this method can cause the cookie to be written to disk immediately.