Skip to content

Commit

Permalink
Add set-local-storage-item/trusted-set-local-storage-item scriptlets
Browse files Browse the repository at this point in the history
Related discussion:
- uBlockOrigin/uBlock-issues#2697
  • Loading branch information
gorhill committed Jun 20, 2023
1 parent db9662f commit 9443ba8
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2765,6 +2765,61 @@ function setCookie(
);
}

/*******************************************************************************
*
* set-local-storage-item.js
*
* Set a local storage entry to a specific, allowed value.
*
* Reference:
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-local-storage-item.js
*
**/

builtinScriptlets.push({
name: 'set-local-storage-item.js',
fn: setLocalStorageItem,
world: 'ISOLATED',
});
function setLocalStorageItem(
key = '',
value = ''
) {
if ( key === '' ) { return; }
if ( value === '' ) { return; }

let actualValue;
if ( value === 'undefined' ) {
actualValue = undefined;
} else if ( value === 'false' ) {
actualValue = false;
} else if ( value === 'true' ) {
actualValue = true;
} else if ( value === 'null' ) {
actualValue = null;
} else if ( value === '{}' ) {
actualValue = '{}';
} else if ( value === '[]' ) {
actualValue = '[]';
} else if ( value === "''" ) {
actualValue = '';
} else if ( value === 'yes' ) {
actualValue = 'yes';
} else if ( value === 'no' ) {
actualValue = 'no';
} else if ( /^\d+$/.test(value) ) {
actualValue = parseInt(value, 10);
if ( actualValue > 32767 ) { return; }
} else {
return;
}

try {
self.localStorage.setItem(key, `${actualValue}`);
} catch(ex) {
}
}

/*******************************************************************************
*
* Scriplets below this section are only available for filter lists from
Expand Down Expand Up @@ -2903,4 +2958,41 @@ function trustedSetCookie(
);
}

/*******************************************************************************
*
* trusted-set-local-storage-item.js
*
* Set a local storage entry to an arbitrary value.
*
* Reference:
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/trusted-set-local-storage-item.js
*
**/

builtinScriptlets.push({
name: 'trusted-set-local-storage-item.js',
requiresTrust: true,
fn: trustedSetLocalStorageItem,
world: 'ISOLATED',
});
function trustedSetLocalStorageItem(
key = '',
value = ''
) {
if ( key === '' ) { return; }
if ( value === '' ) { return; }

let actualValue = value;
if ( value === '$now$' ) {
actualValue = Date.now();
} else if ( value === '$currentDate$' ) {
actualValue = `${Date()}`;
}

try {
self.localStorage.setItem(key, `${actualValue}`);
} catch(ex) {
}
}

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

0 comments on commit 9443ba8

Please sign in to comment.