Files
RSSHub/lib/utils/playwright-utils.ts
T
dependabot[bot]andTonyRL 6bce9493b5 chore(deps-dev): bump eslint-plugin-unicorn from 71.1.0 to 72.0.0 (#22723)
* chore(deps-dev): bump eslint-plugin-unicorn from 71.1.0 to 72.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 71.1.0 to 72.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v71.1.0...v72.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 72.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix prefer-dom-node-html-methods

* style: fix prefer-split-limit

* style: fix no-unnecessary-string-trim

* style: fix no-multiple-promise-resolver-calls

* style: add v72 preset

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
2026-07-15 23:11:15 +08:00

59 lines
2.4 KiB
TypeScript

/**
* Get Cookie-header-style cookie string from a browser cookie array.
*
* @param cookies Browser cookie array
* @param {RegExp | string} domainFilter Filter cookies by domain or RegExp
* @return {string} Cookie-header-style cookie string (e.g. "foobar; foo=bar; baz=qux")
*/
const parseCookieArray = (cookies, domainFilter?: string | RegExp) => {
if (typeof domainFilter === 'string') {
const dotDomain = '.' + domainFilter;
cookies = cookies.filter(({ domain }) => domain === domainFilter || domain.endsWith(dotDomain));
} else if (domainFilter && domainFilter.test !== undefined) {
cookies = cookies.filter(({ domain }) => domainFilter.test(domain));
}
// {name: '', value: 'foobar'} => 'foobar' // https://stackoverflow.com/questions/42531198/cookie-without-a-name
// {name: 'foo', value: 'bar'} => 'foo=bar'
return cookies.map(({ name, value }) => (name ? `${name}=${value}` : value)).join('; ');
};
/**
* Construct a browser cookie array from a Cookie-header-style cookie string.
*
* @param {string} cookieStr Cookie-header-style cookie string (e.g. "foobar; foo=bar; baz=qux")
* @param {string} domain Domain to set for each cookie
* @return Browser cookie array
*/
const constructCookieArray = (cookieStr, domain) =>
cookieStr.split('; ').map((item) => {
const [name, value] = item.split('=', 2);
return value === undefined ? { name: '', value: name, domain, path: '/' } : { name, value, domain, path: '/' };
});
/**
* Set cookies for a page
*
* @param page Browser Page object
* @param {string} cookieStr Cookie-header-style cookie string (e.g. "foobar; foo=bar; baz=qux")
* @param {string} domain Domain to set for each cookie
* @return {Promise<void>}
*/
const setCookies = async (page, cookieStr, domain) => {
const cookies = constructCookieArray(cookieStr, domain);
await page.context().addCookies(cookies);
};
/**
* Get Cookie-header-style cookie string from a page
*
* @param page Browser Page object
* @param {RegExp | string} domainFilter Filter cookies by domain or RegExp
* @return {Promise<string>} Cookie-header-style cookie string
*/
const getCookies = async (page, domainFilter?: string) => {
const cookies = await page.context().cookies();
return parseCookieArray(cookies, domainFilter);
};
export { constructCookieArray, getCookies, parseCookieArray, setCookies };