Table of Contents Previous entry (api:GM_webRequest) Next entry (api:GM_audio.setMute)
GM_cookie.list(details[, callback])
Note: httpOnly cookies are supported at the BETA versions of Tampermonkey only for now
Tampermonkey checks if the script has @include or @match access to given details.url arguments!
Parameters:
- details object, containing properties of the cookies to retrieve
- url string?, representing the URL to retrieve cookies from (defaults to current document URL)
- domain string?, representing the domain of the cookies to retrieve
- name string?, representing the name of the cookies to retrieve
- path string?, representing the path of the cookies to retrieve
- partitionKey v5.2+ object?, representing the partition key of the cookies, use an empty object to retrieve all cookies
- topLevelSite string?, representing the top frame site of the cookies
- callback function?, to be called when the cookies have been retrieved. The function will be passed two arguments:
- cookies object[], representing the retrieved cookies
- error string, representing an error message if an error occurred, null otherwise.
The cookie objects have the following properties:
- domain string, representing the domain of the cookie
- expirationDate number?, the expiration date of the cookie in seconds since the Unix epoch. If not specified, the cookie never expires.
- firstPartyDomain string?: the first party domain of the cookie.
- partitionKey v5.2+ object?, containing the partition key of the cookie
- topLevelSite string?, representing the top frame site of the cookie
- hostOnly boolean, indicating whether the cookie is a host-only cookie
- httpOnly boolean, indicating whether the cookie is an HTTP-only cookie
- name string, representing the name of the cookie
- path string, representing the path of the cookie
- sameSite string, indicating the SameSite attribute of the cookie
- secure boolean, indicating whether the cookie requires a secure connection
- session boolean, indicating whether the cookie is a session cookie
- value string, representing the value of the cookie
Example usage:
// Retrieve all cookies with name "mycookie"
GM_cookie.list({ name: "mycookie" }, function(cookies, error) {
if (!error) {
console.log(cookies);
} else {
console.error(error);
}
});
// Retrieve all cookies for the current domain
const cookies = await GM.cookie.list()
console.log(cookies);
GM_cookie.set(details[, callback])
Sets a cookie with the given details. Supported properties are defined here.
Parameters:
- details: An object containing the details of the cookie to be set. The object can have the following properties:
- url string?, the URL to associate the cookie with. If not specified, the cookie is associated with the current document's URL.
- name string, the name of the cookie.
- value string, the value of the cookie.
- domain string?, the domain of the cookie.
- firstPartyDomain string?: the first party domain of the cookie.
- partitionKey v5.2+ object?, containing the partition key of the cookie
- topLevelSite string?, representing the top frame site of the cookie
- path string?, the path of the cookie.
- secure boolean?, whether the cookie should only be sent over HTTPS.
- httpOnly boolean?, whether the cookie should be marked as HttpOnly.
- expirationDate number?, the expiration date of the cookie in seconds since the Unix epoch. If not specified, the cookie never expires.
- callback function?, a function to be called when the operation is complete. The function is passed one argument:
- error string?, if there was an error setting the cookie, this contains an error message. Otherwise, it is
undefined.
Example:
GM_cookie.set({
url: 'https://example.com',
name: 'name',
value: 'value',
domain: '.example.com',
path: '/',
secure: true,
httpOnly: true,
expirationDate: Math.floor(Date.now() / 1000) + (60 * 60 * 24 * 30) // Expires in 30 days
}, function(error) {
if (error) {
console.error(error);
} else {
console.log('Cookie set successfully.');
}
});
GM.cookie.set({
name: 'name',
value: 'value'
})
.then(() => {
console.log('Cookie set successfully.');
})
.catch((error) => {
console.error(error);
});
GM_cookie.delete(details, callback)
Deletes a cookie.
Parameters:
The details object can have the following properties:
- url string?, the URL associated with the cookie. If
url is not specified, the current document's URL will be used.
- name string, the name of the cookie to delete.
- firstPartyDomain string?: the first party domain of the cookie to delete.
- partitionKey v5.2+ object?, representing the partition key of the cookie to delete
- topLevelSite string?, representing the top frame site of the cookies
The callback function is optional and will be called when the cookie has been deleted or an error has occurred. It takes one argument:
- error string?, an error message, or
undefined if the cookie was deleted successfully.
Example:
GM_cookie.delete({ name: 'cookie_name' }, function(error) {
if (error) {
console.error(error);
} else {
console.log('Cookie deleted successfully');
}
});