From 27a54c084556f657522b06484d2e28b21e1fac5a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 15 Jun 2023 11:08:35 -0400 Subject: [PATCH] Add `set-cookie` scriptlet This new scriptlet is only valid when used in a trusted lists. Implementation follows: https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js#L16 --- assets/resources/scriptlets.js | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index df070abf7..dfa3d61f2 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -2629,4 +2629,51 @@ function trustedSetConstant( setConstantCore(true, ...args); } +/******************************************************************************* + * + * set-cookie.js + * + * Set specified cookie to a specific value. + * + * Reference: + * https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js + * + **/ + +builtinScriptlets.push({ + name: 'set-cookie.js', + requiresTrust: true, + fn: setCookie, + world: 'ISOLATED', +}); +function setCookie( + name = '', + value = '', + path = '/' +) { + if ( name === '' ) { return; } + const validValues = new Set([ + 'true', 'True', + 'false', 'False', + 'yes', 'Yes', 'y', 'Y', + 'no', 'No', 'n', 'N', + 'ok', 'OK', + ]); + if ( validValues.has(value) === false ) { + if ( /^\d+$/.test(value) === false ) { return; } + const n = parseInt(value, 10); + if ( n < 0 || n > 15 ) { return; } + } + const validPaths = new Set([ '/', 'none' ]); + if ( validPaths.has(path) === false ) { return; } + const cookieParts = [ + encodeURIComponent(name), '=', + encodeURIComponent(value), + ]; + if ( path !== 'none' ) { + cookieParts.push('; path=/'); + } + document.cookie = cookieParts.join(''); +} + /******************************************************************************/