Backup/restore only modified advanced settings

This reduces the size of the backup file and also
ensures that default values can be changed.
This commit is contained in:
Raymond Hill 2020-10-03 12:34:21 -04:00
parent 01de814483
commit f4aebc9390
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 22 additions and 6 deletions

View File

@ -886,7 +886,7 @@ const backupUserData = async function() {
version: vAPI.app.version, version: vAPI.app.version,
userSettings: µb.userSettings, userSettings: µb.userSettings,
selectedFilterLists: µb.selectedFilterLists, selectedFilterLists: µb.selectedFilterLists,
hiddenSettings: µb.hiddenSettings, hiddenSettings: µb.getModifiedHiddenSettings(),
whitelist: µb.arrayFromWhitelist(µb.netWhitelist), whitelist: µb.arrayFromWhitelist(µb.netWhitelist),
// String representation eventually to be deprecated // String representation eventually to be deprecated
netWhitelist: µb.stringFromWhitelist(µb.netWhitelist), netWhitelist: µb.stringFromWhitelist(µb.netWhitelist),
@ -927,12 +927,24 @@ const restoreUserData = async function(request) {
// Restore user data // Restore user data
vAPI.storage.set(userData.userSettings); vAPI.storage.set(userData.userSettings);
// Restore advanced settings.
let hiddenSettings = userData.hiddenSettings; let hiddenSettings = userData.hiddenSettings;
if ( hiddenSettings instanceof Object === false ) { if ( hiddenSettings instanceof Object === false ) {
hiddenSettings = µBlock.hiddenSettingsFromString( hiddenSettings = µBlock.hiddenSettingsFromString(
userData.hiddenSettingsString || '' 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 // Whitelist directives can be represented as an array or as a
// (eventually to be deprecated) string. // (eventually to be deprecated) string.
let whitelist = userData.whitelist; let whitelist = userData.whitelist;
@ -944,7 +956,7 @@ const restoreUserData = async function(request) {
whitelist = userData.netWhitelist.split('\n'); whitelist = userData.netWhitelist.split('\n');
} }
vAPI.storage.set({ vAPI.storage.set({
hiddenSettings: hiddenSettings, hiddenSettings,
netWhitelist: whitelist || [], netWhitelist: whitelist || [],
dynamicFilteringString: userData.dynamicFilteringString || '', dynamicFilteringString: userData.dynamicFilteringString || '',
urlFilteringString: userData.urlFilteringString || '', urlFilteringString: userData.urlFilteringString || '',

View File

@ -119,17 +119,21 @@
// This way the new default values in the future will properly apply for those // This way the new default values in the future will properly apply for those
// which were not modified by the user. // which were not modified by the user.
µBlock.saveHiddenSettings = function() { µBlock.getModifiedHiddenSettings = function() {
const bin = { hiddenSettings: {} }; const out = {};
for ( const prop in this.hiddenSettings ) { for ( const prop in this.hiddenSettings ) {
if ( if (
this.hiddenSettings.hasOwnProperty(prop) && this.hiddenSettings.hasOwnProperty(prop) &&
this.hiddenSettings[prop] !== this.hiddenSettingsDefault[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', ( ) => { self.addEventListener('hiddenSettingsChanged', ( ) => {