Skip to content

Commit

Permalink
Add set-cookie scriptlet
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gorhill committed Jun 15, 2023
1 parent e5bd755 commit 27a54c0
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}

/******************************************************************************/

0 comments on commit 27a54c0

Please sign in to comment.