From 9443ba80a06c8748fc5164bb0ad6da752a8b3bf3 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 20 Jun 2023 10:58:10 -0400 Subject: [PATCH] Add `set-local-storage-item`/`trusted-set-local-storage-item` scriptlets Related discussion: - https://github.com/uBlockOrigin/uBlock-issues/discussions/2697 --- assets/resources/scriptlets.js | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index e30e26c67..0d6e9c65f 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -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) { + } +} + /******************************************************************************/