Pergunta

The Chrome extension needs a list of strings specified in its settings. I want to provide default strings if the extension is installed, but allow the user to change the list of strings as he wishes. How can I provide these inital settings in the chrome.storage API?

My first idea was to check whether the list is empty. If it is, fill it with the default values. However, the user can delete all strings in the list if he wants to which would cause the list to be filled with the default elements again.

Foi útil?

Solução

You can give default values to get() function. At extension initialization, you can do:

chrome.storage.local.get({option: "default"}, function(data){
  chrome.storage.local.set(data, /*...*/);
});

This will set the value to "default" if absent and conserve current value if present.


Aha, I see the deleted key dilemma.

There are two options:

  1. Avoid deleting keys. Set a key to undefined, or empty string, but keep the key.

  2. Initialize with defaults and then set {settings_schema: 1}. This way you can check for options schema version on initialization, and perform upgrade as needed (i.e. add defaults for a new option).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top