目录

Cookies

The cookie store of the session.

import { session } from '@mobrowser/api';

Example 

import { session } from '@mobrowser/api';

await session.cookies.set({
  url: 'https://example.com',
  name: 'theme',
  value: 'dark'
})

const stored = await session.cookies.get({ url: 'https://example.com' })
console.log(stored.map((cookie) => cookie.name))

Methods 

get() 

get(filter?: CookieFilter): Promise<Cookie[]>;

Reads the cookies that match filter.

Called without a filter, it returns every cookie in the store.

ParameterTypeDescription
filter?CookieFilterNarrows which cookies are returned. Every cookie matches when omitted.

Return value 

A promise that resolves with the matching cookies. It is rejected when filter.url is not a valid URL. @throws TypeError if filter is not an object, or if one of its fields has the wrong type.

Example 

import { session } from '@mobrowser/api';

const all = await session.cookies.get()
const forSite = await session.cookies.get({ url: 'https://example.com' })
const named = await session.cookies.get({ name: 'theme' })

set() 

set(details: CookieDetails): Promise<void>;

Stores a cookie, replacing any existing cookie with the same name, domain, and path.

ParameterTypeDescription
detailsCookieDetailsThe cookie to store.

Return value 

A promise that resolves once the cookie is stored. It is rejected when details.url is not valid, or when the cookie is refused — for example a sameSite: 'noRestriction' cookie that is not also secure. @throws TypeError if details has no url, if one of its fields has the wrong type, or if sameSite is not one of the CookieSameSite values.

Example 

import { session } from '@mobrowser/api';

await session.cookies.set({
  url: 'https://example.com',
  name: 'token',
  value: 'abc123',
  httpOnly: true,
  secure: true
})

remove() 

remove(url: string, name: string): Promise<void>;

Removes the cookie with the given name that matches url.

Removing a cookie that is not in the store succeeds and does nothing.

ParameterTypeDescription
urlstringThe URL the cookie applies to.
namestringThe name of the cookie to remove.

Return value 

A promise that resolves once the cookie is removed. It is rejected when url is not a valid URL.

Example 

import { session } from '@mobrowser/api';

await session.cookies.remove('https://example.com', 'token')

flushStore() 

flushStore(): Promise<void>;

Writes the cookie store to disk.

Persistent cookies are written out on their own schedule, so this is only needed when the application has to be certain they have landed — before quitting through an unusual path, for instance.

Return value 

A promise that resolves once the store has been written.

Example 

import { session } from '@mobrowser/api';

await session.cookies.flushStore()

Events 

‘changed’ 

on(event: 'changed', listener: (change: CookieChange) => void): void;
off(event: 'changed', listener: (change: CookieChange) => void): void;

Emitted when any cookie in the store is added, updated, or removed.

The listener is called for changes made through this API as well as for those made by loaded pages and by the network stack.

Example 

import { session } from '@mobrowser/api';

session.cookies.on('changed', ({ cookie, cause, removed }) => {
  console.log(`${cookie.name} ${removed ? 'removed' : 'set'} (${cause})`)
})