From e05fefc8317ba3e5fc3c2f85478e400ee8f97f7c Mon Sep 17 00:00:00 2001 From: Slava Leleka Date: Tue, 25 Oct 2022 15:26:21 +0300 Subject: [PATCH] add path parameter to set cookie scriptlets --- src/helpers/prepare-cookie.js | 9 ++++++--- src/scriptlets/set-cookie-reload.js | 14 ++++++++++---- src/scriptlets/set-cookie.js | 15 ++++++++++----- wiki/about-scriptlets.md | 21 ++++++++++++++++----- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/helpers/prepare-cookie.js b/src/helpers/prepare-cookie.js index 29e1abb2..50227c76 100644 --- a/src/helpers/prepare-cookie.js +++ b/src/helpers/prepare-cookie.js @@ -3,10 +3,11 @@ import { nativeIsNaN } from './number-utils'; * Prepares cookie string if given parameters are ok * @param {string} name cookie name to set * @param {string} value cookie value to set + * @param {string} path cookie path to set, no set for 'none' * @returns {string|null} cookie string if ok OR null if not */ -export const prepareCookie = (name, value) => { - if (!name || !value) { +export const prepareCookie = (name, value, path) => { + if (!name || !value || !path) { return null; } @@ -43,7 +44,9 @@ export const prepareCookie = (name, value) => { return null; } - const pathToSet = 'path=/;'; + const pathToSet = path === 'none' + ? '' + : `path=${path}`; // eslint-disable-next-line max-len const cookieData = `${encodeURIComponent(name)}=${encodeURIComponent(valueToSet)}; ${pathToSet}`; diff --git a/src/scriptlets/set-cookie-reload.js b/src/scriptlets/set-cookie-reload.js index 97a14c75..037c589d 100644 --- a/src/scriptlets/set-cookie-reload.js +++ b/src/scriptlets/set-cookie-reload.js @@ -8,12 +8,13 @@ import { * @scriptlet set-cookie-reload * * @description - * Sets a cookie with the specified name and value, and then reloads the current page. + * Sets a cookie with the specified name and value, and path, + * and reloads the current page after the cookie setting. * If reloading option is not needed, use [set-cookie](#set-cookie) scriptlet. * * **Syntax** * ``` - * example.org#%#//scriptlet('set-cookie-reload', name, value) + * example.org#%#//scriptlet('set-cookie-reload', name, value[, path]) * ``` * * - `name` - required, cookie name to be set @@ -25,15 +26,20 @@ import { * - `yes` / `Yes` / `Y` * - `no` * - `ok` / `OK` + * - `path` - optional, cookie path, defaults to `/`; possible values: + * - `/` — root path + * - `none` — to set no path at all * * **Examples** * ``` * example.org#%#//scriptlet('set-cookie-reload', 'checking', 'ok') * * example.org#%#//scriptlet('set-cookie-reload', 'gdpr-settings-cookie', '1') + * + * example.org#%#//scriptlet('set-cookie-reload', 'cookie-set', 'true', 'none') * ``` */ -export function setCookieReload(source, name, value) { +export function setCookieReload(source, name, value, path = '/') { const isCookieSetWithValue = (name, value) => { return document.cookie.split(';') .some((cookieStr) => { @@ -52,7 +58,7 @@ export function setCookieReload(source, name, value) { return; } - const cookieData = prepareCookie(name, value); + const cookieData = prepareCookie(name, value, path); if (cookieData) { document.cookie = cookieData; diff --git a/src/scriptlets/set-cookie.js b/src/scriptlets/set-cookie.js index d6f23ad7..441b368e 100644 --- a/src/scriptlets/set-cookie.js +++ b/src/scriptlets/set-cookie.js @@ -5,11 +5,11 @@ import { hit, nativeIsNaN, prepareCookie } from '../helpers/index'; * @scriptlet set-cookie * * @description - * Sets a cookie with the specified name and value. Cookie path defaults to root. + * Sets a cookie with the specified name, value, and path. * * **Syntax** * ``` - * example.org#%#//scriptlet('set-cookie', name, value) + * example.org#%#//scriptlet('set-cookie', name, value[, path]) * ``` * * - `name` - required, cookie name to be set @@ -21,17 +21,22 @@ import { hit, nativeIsNaN, prepareCookie } from '../helpers/index'; * - `yes` / `Yes` / `Y` * - `no` * - `ok` / `OK` + * - `path` - optional, cookie path, defaults to `/`; possible values: + * - `/` — root path + * - `none` — to set no path at all * * **Examples** * ``` - * example.org#%#//scriptlet('set-cookie', 'ReadlyCookieConsent', '1') + * example.org#%#//scriptlet('set-cookie', 'CookieConsent', '1') * * example.org#%#//scriptlet('set-cookie', 'gdpr-settings-cookie', 'true') + * + * example.org#%#//scriptlet('set-cookie', 'cookie_consent', 'ok', 'none') * ``` */ /* eslint-enable max-len */ -export function setCookie(source, name, value) { - const cookieData = prepareCookie(name, value); +export function setCookie(source, name, value, path = '/') { + const cookieData = prepareCookie(name, value, path); if (cookieData) { hit(source); diff --git a/wiki/about-scriptlets.md b/wiki/about-scriptlets.md index 847dad79..9bd3cfc2 100644 --- a/wiki/about-scriptlets.md +++ b/wiki/about-scriptlets.md @@ -1554,12 +1554,13 @@ example.org#%#//scriptlet('set-constant', 'document.third', 'trueFunc', 'checkin ### ⚡️ set-cookie-reload -Sets a cookie with the specified name and value, and then reloads the current page. +Sets a cookie with the specified name and value, and path, +and reloads the current page after the cookie setting. If reloading option is not needed, use [set-cookie](#set-cookie) scriptlet. **Syntax** ``` -example.org#%#//scriptlet('set-cookie-reload', name, value) +example.org#%#//scriptlet('set-cookie-reload', name, value[, path]) ``` - `name` - required, cookie name to be set @@ -1571,12 +1572,17 @@ example.org#%#//scriptlet('set-cookie-reload', name, value) - `yes` / `Yes` / `Y` - `no` - `ok` / `OK` +- `path` - optional, cookie path, defaults to `/`; possible values: + - `/` — root path + - `none` — to set no path at all **Examples** ``` example.org#%#//scriptlet('set-cookie-reload', 'checking', 'ok') example.org#%#//scriptlet('set-cookie-reload', 'gdpr-settings-cookie', '1') + +example.org#%#//scriptlet('set-cookie-reload', 'cookie-set', 'true', 'none') ``` [Scriptlet source](../src/scriptlets/set-cookie-reload.js) @@ -1584,11 +1590,11 @@ example.org#%#//scriptlet('set-cookie-reload', 'gdpr-settings-cookie', '1') ### ⚡️ set-cookie -Sets a cookie with the specified name and value. Cookie path defaults to root. +Sets a cookie with the specified name, value, and path. **Syntax** ``` -example.org#%#//scriptlet('set-cookie', name, value) +example.org#%#//scriptlet('set-cookie', name, value[, path]) ``` - `name` - required, cookie name to be set @@ -1600,12 +1606,17 @@ example.org#%#//scriptlet('set-cookie', name, value) - `yes` / `Yes` / `Y` - `no` - `ok` / `OK` +- `path` - optional, cookie path, defaults to `/`; possible values: + - `/` — root path + - `none` — to set no path at all **Examples** ``` -example.org#%#//scriptlet('set-cookie', 'ReadlyCookieConsent', '1') +example.org#%#//scriptlet('set-cookie', 'CookieConsent', '1') example.org#%#//scriptlet('set-cookie', 'gdpr-settings-cookie', 'true') + +example.org#%#//scriptlet('set-cookie', 'cookie_consent', 'ok', 'none') ``` [Scriptlet source](../src/scriptlets/set-cookie.js)