From: Mike Frysinger Date: Fri, 7 Oct 2022 18:08:59 +0000 (+0545) Subject: migrate from localStorage to chrome.storage.local X-Git-Tag: v0.3~4 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=d9e77fbf08e2e408b3289e0a559859bc98221e12;p=chrome-ext%2Ftabs-backup.git migrate from localStorage to chrome.storage.local This storage type isn't available in MV3, so we have to migrate to chrome.storage.local for all settings. --- diff --git a/background.js b/background.js index 0c90a71..1771059 100644 --- a/background.js +++ b/background.js @@ -1,15 +1,35 @@ - -// Set default values if needed -if (!localStorage.prefsMaxBackupItems) { - localStorage.prefsMaxBackupItems = "10"; +// Migrate old localStorage to chrome.storage.local. +if (localStorage.prefsMaxBackupItems !== undefined) { + chrome.storage.local.set({ + prefs_max_backup_items: parseInt(localStorage.prefsMaxBackupItems), + }); + localStorage.removeItem("prefsMaxBackupItems"); } - -if (!localStorage.prefsBackupTimer) { - localStorage.prefsBackupTimer = "30"; +if (localStorage.prefsBackupTimer !== undefined) { + chrome.storage.local.set({ + prefs_backup_timer: parseInt(localStorage.prefsBackupTimer), + }); + localStorage.removeItem("prefsBackupTimer"); +} +if (localStorage.prefsTheme !== undefined) { + chrome.storage.local.set({ + prefs_theme: localStorage.prefsTheme, + }); + localStorage.removeItem("prefsTheme"); } +localStorage.removeItem("lastBackupTime"); -// Create a backup on first install (or if storage is wiped for some reason). -chrome.storage.local.get("backups_list", function(items) { +// Create a backup on first install (or if storage is wiped for some reason. +chrome.storage.local.get(function(items) { + // Setup defaults. + if (items.prefs_max_backup_items === undefined) { + chrome.storage.local.set({prefs_max_backup_items: 10}); + } + if (items.prefs_backup_timer === undefined) { + chrome.storage.local.set({prefs_backup_timer: 30}); + } + + // If a backup exists already, nothing to do. if (items.backups_list) { return; } @@ -31,8 +51,10 @@ function initAlarm () { // Clear any previous alarm chrome.alarms.clearAll(); - var timerMinutes = parseInt(localStorage.prefsBackupTimer); - chrome.alarms.create(BACKUP_ALARM_NAME, {periodInMinutes: timerMinutes}); + chrome.storage.local.get(function(items) { + const timerMinutes = items.prefs_backup_timer; + chrome.alarms.create(BACKUP_ALARM_NAME, {periodInMinutes: timerMinutes}); + }); } initAlarm(); @@ -89,13 +111,13 @@ function backupNowManual (callbackDone) { } function deleteOldestBackup () { - chrome.storage.local.get("backups_list", function(items) { + chrome.storage.local.get(function(items) { if(!items.backups_list) { return; } var backupsList = items.backups_list; - var numItemsToDelete = backupsList.length - parseInt(localStorage.prefsMaxBackupItems); + var numItemsToDelete = backupsList.length - items.prefs_max_backup_items; if (numItemsToDelete > 0) { var i = 0; var loopFunc = function () { @@ -229,7 +251,7 @@ function backupNow(isAutomatic, backupName, callbackDone) { console.log("backup saved"); //alert("Backup saved successfully!"); - chrome.storage.local.get("backups_list", function(items) { + chrome.storage.local.get(function(items) { var backupsList = []; if(items.backups_list) { backupsList = items.backups_list; @@ -252,7 +274,7 @@ function backupNow(isAutomatic, backupName, callbackDone) { updateBrowserActionIcon (0); callbackDone(true, backupName, fullBackup); - if (backupsList.length > parseInt(localStorage.prefsMaxBackupItems)) { + if (backupsList.length > items.prefs_max_backup_items) { deleteOldestBackup(); } } diff --git a/options.js b/options.js index e014be8..5760dbc 100644 --- a/options.js +++ b/options.js @@ -54,14 +54,11 @@ function setThemeValue(value) { } function saveOptions () { - var backupPeriodMinutes = getSelectValue("prefsSelectBackupPeriod"); - localStorage.prefsBackupTimer = backupPeriodMinutes; - - var backupMaxItems = getSelectValue("prefsSelectMaxBackups"); - localStorage.prefsMaxBackupItems = backupMaxItems; - - var theme = document.getElementById('prefsTheme').value; - localStorage.prefsTheme = theme; + chrome.storage.local.set({ + prefs_backup_timer: getSelectValue("prefsSelectBackupPeriod"), + prefs_max_backup_items: getSelectValue("prefsSelectMaxBackups"), + prefs_theme: document.getElementById('prefsTheme').value, + }); // Re-initialize the backup alarm chrome.runtime.getBackgroundPage((bg) => bg.initAlarm()); @@ -84,11 +81,9 @@ function restoreToDefault() { } function restoreOptions() { - var backupPeriodMinutes = localStorage.prefsBackupTimer; - var backupMaxItems = localStorage.prefsMaxBackupItems; - var theme = localStorage.prefsTheme; - - setSelectValue ("prefsSelectBackupPeriod", backupPeriodMinutes); - setSelectValue ("prefsSelectMaxBackups", backupMaxItems); - setThemeValue(theme); + chrome.storage.local.get(function(items) { + setSelectValue("prefsSelectBackupPeriod", items.prefs_backup_timer); + setSelectValue("prefsSelectMaxBackups", items.prefs_max_backup_items); + setThemeValue(items.prefs_theme); + }); } diff --git a/theme-init.js b/theme-init.js index ade6ae2..16fd81c 100644 --- a/theme-init.js +++ b/theme-init.js @@ -7,5 +7,6 @@ function switchTheme(theme) { css.href = ''; } } - -switchTheme(localStorage.prefsTheme); +chrome.storage.local.get(function(items) { + switchTheme(items.prefs_theme); +});