4

I have a javascript Array which I am stringifying in order to store it in localstorage

console.log(request.keywords.length);
localStorage.keywords = JSON.stringify(request.keywords);

where keywords is javascript array. Here request.keywords.length returns 12 which is number of elements in array.

After retrieving it and parsing it back to JSON

 var keywords = chrome.extension.getBackgroundPage().getItem("keywords");
    var kjos=JSON.parse(keywords);
    console.log(kjos.length);

The length returned is 342 which is the length of whole string. I tried getting type of object via constructor.name property, it gives me string instead of Array.

Any ideas what is going wrong ?

Snippets: Background.html

 function getItem(key) {
    var value;
    log('Get Item:' + key);
    try {
      value = window.localStorage.getItem(key);
    }catch(e) {
      log("Error inside getItem() for key:" + key);
      log(e);
      value = "null";
    }
    log("Returning value: " + value);
    return value;
  }

/////

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
localStorage.keywords = JSON.stringify(request.keywords);
}
);

//////////// Popup.js

var keywords = chrome.extension.getBackgroundPage().getItem("keywords");

    var kjos=JSON.parse(keywords); //kjos is a string variable

///// keywords.js

//keywordsArray is an Array object
    // Message passing to background page
        chrome.extension.sendRequest({message: "setKeywords", keywords: keywordsArray}, function() 
        {
            console.log(keywordsArray);
            console.log("message sent");
          // The data has been sent, we can close the window now.
          //window.close();
        });
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • 2
    Can you log what ends up in `keywords` right after this line: `localStorage.keywords = JSON.stringify(request.keywords);` – nnnnnn Jun 22 '11 at 07:19
  • Its stringified notation, for ex "[\"Google\", \"Account Options\", \"Search Options\", \"Search Results\", \"jQuery.getJSON() – jQuery API\", \"How do you parse in JavaScript an array that looks like this ...\", \"Get more discussion results\"] – Madhur Ahuja Jun 22 '11 at 07:23
  • can you print out the elements of keywords array? – Adithya Surampudi Jun 22 '11 at 07:26
  • Its a big list, but its standard array – Madhur Ahuja Jun 22 '11 at 07:31
  • Why are you stringifying it in the first place? localStorage isn't limited to strings. – generalhenry Jun 22 '11 at 07:32
  • 1
    This suggests otherwise http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage – Madhur Ahuja Jun 22 '11 at 07:40
  • Try to temporarily avoid the localstorage and do a JSON.parse() on the same, but hardcoded string. Is the result still the same? If not, then perhaps you are `stringify`ing twice? – Udo G Jun 22 '11 at 08:51
  • Where do you run your first piece of code - in a background page? What about second one? Also can you show what your `getItem()` function does. – serg Jun 22 '11 at 15:10
  • @serg: I have added the snippets in my post – Madhur Ahuja Jun 22 '11 at 16:37
  • Quiet weird, I tried it in my Chrome and everything works fine. Try to look what is inside `localStorage` right after you call `localStorage.keywords = JSON.stringify(request.keywords);` in Chrome's Developer Tools or just type into console `localStorage`. – martin Jun 23 '11 at 13:51

1 Answers1

1

You need to use request like this - chrome.extension.sendRequest({message: "setKeywords"..., but for getKeywords operation. Function getItem can not be used for access to a variable of background page.

Yuck
  • 49,664
  • 13
  • 105
  • 135
Evgeny Karpov
  • 2,386
  • 26
  • 16