0

am a newbie, trying to write some basics extension. For my extension to work i need to initialize some data, so what I did is inside my background.js i declared something like this.

localStorage["frequency"] = 1; //I want one as Default value. This line is not inside any method, its just the first line of the file background.js

Users can goto Options page and change this above variable to any value using the GUI. As soon as the user changes it in UI am updating that value.

Now the problem is to my understanding background.js reloads everytime the machine is restarted. So every time I restart my machine and open Chrome the frequency value is changed back to 1. In order to avoid this where I need to initialize this value?

Srikanth
  • 836
  • 10
  • 20

3 Answers3

1

You could just use a specific default key. So if frequency is not set you would try default-frequency. The default keys are then still set or defined in the background.js.

I like to do that in one step, in a function like this

function storageGet(key,defaultValue){
    var item = localstorage.getItem(key);
    if(item === null)return defaultValue;
    else return item;
}

(According to the specification localstorage must return null if no value has been set.) So for your case it would look something like

var f = storageGet("frequency",1);

Furthermore you might be interested in checking out the chrome.storage API. It's used similar to localstorage but provides additional functionalities which might be useful for your extension. In particular it supports to synchronize the user data across different chrome browsers.

edit I changed the if statement in regard to apsillers objection. But since the specification says it's ought to be null, I think it makes sense to check for that instead of undefined.

LeoR
  • 666
  • 1
  • 6
  • 20
  • 1
    +1; however, you may want to use `if(typeof item != "undefined")` in your check. If `getItem` is [wrapped to support object storage](http://stackoverflow.com/a/2010948/710446) and the program stores `false` (or any [falsey value](http://stackoverflow.com/questions/3982663/falsey-values-in-javascript)), then the retrieval will fail and use the default value. – apsillers Aug 30 '12 at 14:10
  • Thanks for the quick answer. Will try to implement. – Srikanth Aug 30 '12 at 14:16
  • @LeoR Right, good catch! Property access on a non-existent key yields `undefined`, whereas function access yields `null`, as you said. – apsillers Aug 30 '12 at 18:43
1

This is another solution:

// background.js

initializeDefaultValues();

function initializeDefaultValues() {
    if (localStorage.getItem('default_values_initialized')) {
        return;
    }

    // set default values for your variable here
    localStorage.setItem('frequency', 1);

    localStorage.setItem('default_values_initialized', true);
}
Jacob Dam
  • 2,278
  • 18
  • 18
  • is there any syntax error in here? localStorage.setItem('frequency') = 1; shows Uncaught ReferenceError: Invalid left-hand side in assignment , so I changed it to localStorage.setItem['frequency'] = 1; no errors displayed but there is no item stored in localStorage. Tried localStorage['frequency'] = 1; worked and displayed in localStorage. Am I doing anything wrong? – Srikanth Aug 30 '12 at 15:28
  • No worries this worked. localStorage.setItem("frequency", 1); – Srikanth Aug 31 '12 at 06:36
0

I think the problem lies with your syntax. To get and set your localStorage values try using this:

// to set
localStorage.setItem("frequency", 1);

// to get
localStorage.getItem("frequency");
Rick van Mook
  • 2,065
  • 1
  • 12
  • 16
  • 2
    `getItem` and `setItem` are virtually identical to property access. The only trivial difference is that property access cannot persistently store on keys that are the names of existing properties of localStorage (e.g., `localStorage["getItem"] = "foo"` does not persist, but `localStorage.setItem("getItem", "foo")` does. One other difference is that the functions can be wrapped to overwrite their default behavior, e.g. to JSONify an object before storage. – apsillers Aug 30 '12 at 14:02