i want to store some data so how can i store it more effectively than local storage

161 views
Skip to first unread message

Vivek Sagar

unread,
Apr 26, 2023, 1:21:32 PM4/26/23
to Chromium Extensions
my extension will let user change some settings  of the extension but the problem is how will the settings get saved if i use local storage i need to put all data in array is there any effective way ?

allSettings=[
    'General',[

    ],
    'Text Colors',[

    ],
    'Side Navbar',[

    ],
    'Navbar',[

    ],
    'Searchbox',[

    ],
    'Chips',[

    ],
    ''
]

Stefan Van Damme

unread,
Apr 26, 2023, 2:22:19 PM4/26/23
to Chromium Extensions, Vivek Sagar
Hi Vivek,

You can save an array with local storage, and also with the chrome.storage API. See this code example on this developer page:

You can implement your Chrome extension using the following JavaScript code:
const defaultSettings = {
  general: {
  },
  textColors: {
  },
  sideNavbar: {
  },
  navbar: {
  },
  searchbox: {
  },
  chips: {
  }
};

// Load the settings from storage, or use the default settings if none exist
chrome.storage.local.get('settings', (result) => {
  const settings = result.settings || defaultSettings;
 
  // Use the settings to configure your extension
  // ...
 
  // Save any changes to the settings
  chrome.storage.local.set({ settings });
});


A tip: If you use chrome.storage.sync.set, the settings will be synced to all of the user's Chrome web browsers.

Thanks,

Stefan Van Damme

unread,
Apr 26, 2023, 2:57:45 PM4/26/23
to Chromium Extensions, Stefan Van Damme, Vivek Sagar
Hi Vivek,

Do then this:
chrome.storage.sync.set({"general": $("general").src, "textColors": $("textColors").value, "sideNavbar": $("sideNavbar").checked, "navbar": $("navbar").checked, "searchbox": $("searchbox").checked
}, "chips": $("chips").checked});
In the above example, I include the checkbox value or input text value in the setting's values.
And if you want to set one item, then do this:
chrome.storage.sync.set({"general": $("general").src});
Note: There is a maximum write for each second, see this developer page:

If you want a real sample, see my Open-Source Print Chrome extension:

Thanks,

On Wed, Apr 26, 2023 at 8:47 PM Vivek Sagar <vivek...@gmail.com> wrote:
But the problem in this is if i want to  just only change or set the general or others setting how will i do i have to again set the whole object in storage 
Reply all
Reply to author
Forward
0 new messages