]> git.wh0rd.org Git - chrome-ext/tabs-backup.git/commitdiff
migrate from localStorage to chrome.storage.local
authorMike Frysinger <vapier@gentoo.org>
Fri, 7 Oct 2022 18:08:59 +0000 (23:53 +0545)
committerMike Frysinger <vapier@gentoo.org>
Fri, 7 Oct 2022 18:15:53 +0000 (00:00 +0545)
This storage type isn't available in MV3, so we have to migrate to
chrome.storage.local for all settings.

background.js
options.js
theme-init.js

index 0c90a71179e59a7816e7305ef353e0adbcca9c86..1771059d1022129b2ceec595ba60860beb0c2b60 100644 (file)
@@ -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();
                                                        }
                                                }
index e014be8c154196c7fc444b5b9807ddb9d7ed3856..5760dbc91ad126611fff4fa851d7a81edb2be519 100644 (file)
@@ -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);
+       });
 }
index ade6ae2ef0a5a807c781d9dfda53f7e9d3faf974..16fd81cee8a9ab5a1b2cb68646058d6534a833b8 100644 (file)
@@ -7,5 +7,6 @@ function switchTheme(theme) {
                css.href = '';
        }
 }
-
-switchTheme(localStorage.prefsTheme);
+chrome.storage.local.get(function(items) {
+       switchTheme(items.prefs_theme);
+});