From f4aebc93904ace45675386ec18f04c7ad7b3d922 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 3 Oct 2020 12:34:21 -0400 Subject: [PATCH] Backup/restore only modified advanced settings This reduces the size of the backup file and also ensures that default values can be changed. --- src/js/messaging.js | 16 ++++++++++++++-- src/js/storage.js | 12 ++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/js/messaging.js b/src/js/messaging.js index 0b541caf5..d6327e6a8 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -886,7 +886,7 @@ const backupUserData = async function() { version: vAPI.app.version, userSettings: µb.userSettings, selectedFilterLists: µb.selectedFilterLists, - hiddenSettings: µb.hiddenSettings, + hiddenSettings: µb.getModifiedHiddenSettings(), whitelist: µb.arrayFromWhitelist(µb.netWhitelist), // String representation eventually to be deprecated netWhitelist: µb.stringFromWhitelist(µb.netWhitelist), @@ -927,12 +927,24 @@ const restoreUserData = async function(request) { // Restore user data vAPI.storage.set(userData.userSettings); + + // Restore advanced settings. let hiddenSettings = userData.hiddenSettings; if ( hiddenSettings instanceof Object === false ) { hiddenSettings = µBlock.hiddenSettingsFromString( userData.hiddenSettingsString || '' ); } + // Discard unknown setting or setting with default value. + for ( const key in hiddenSettings ) { + if ( + this.hiddenSettingsDefault.hasOwnProperty(key) === false || + hiddenSettings[key] === this.hiddenSettingsDefault[key] + ) { + delete hiddenSettings[key]; + } + } + // Whitelist directives can be represented as an array or as a // (eventually to be deprecated) string. let whitelist = userData.whitelist; @@ -944,7 +956,7 @@ const restoreUserData = async function(request) { whitelist = userData.netWhitelist.split('\n'); } vAPI.storage.set({ - hiddenSettings: hiddenSettings, + hiddenSettings, netWhitelist: whitelist || [], dynamicFilteringString: userData.dynamicFilteringString || '', urlFilteringString: userData.urlFilteringString || '', diff --git a/src/js/storage.js b/src/js/storage.js index 75921f92d..79ea86051 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -119,17 +119,21 @@ // This way the new default values in the future will properly apply for those // which were not modified by the user. -µBlock.saveHiddenSettings = function() { - const bin = { hiddenSettings: {} }; +µBlock.getModifiedHiddenSettings = function() { + const out = {}; for ( const prop in this.hiddenSettings ) { if ( this.hiddenSettings.hasOwnProperty(prop) && this.hiddenSettings[prop] !== this.hiddenSettingsDefault[prop] ) { - bin.hiddenSettings[prop] = this.hiddenSettings[prop]; + out[prop] = this.hiddenSettings[prop]; } } - vAPI.storage.set(bin); + return out; +}; + +µBlock.saveHiddenSettings = function() { + vAPI.storage.set({ hiddenSettings: this.getModifiedHiddenSettings() }); }; self.addEventListener('hiddenSettingsChanged', ( ) => {