diff --git a/src/helpers/cookie-utils.js b/src/helpers/cookie-utils.js index 447d6f077..766e1f81b 100644 --- a/src/helpers/cookie-utils.js +++ b/src/helpers/cookie-utils.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 97a14c755..037c589d7 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 d6f23ad75..441b368eb 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 ac43183ba..d7f769e3a 100644 --- a/wiki/about-scriptlets.md +++ b/wiki/about-scriptlets.md @@ -1557,12 +1557,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 @@ -1574,12 +1575,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) @@ -1587,11 +1593,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 @@ -1603,12 +1609,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)