Add `set-local-storage-item`/`trusted-set-local-storage-item` scriptlets

Related discussion:
- https://github.com/uBlockOrigin/uBlock-issues/discussions/2697
This commit is contained in:
Raymond Hill 2023-06-20 10:58:10 -04:00
parent db9662f61b
commit 9443ba80a0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 92 additions and 0 deletions

View File

@ -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
@ -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) {
}
}
/******************************************************************************/