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();
        });
10
  • 2
    Can you log what ends up in keywords right after this line: localStorage.keywords = JSON.stringify(request.keywords); Commented Jun 22, 2011 at 7: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\"] Commented Jun 22, 2011 at 7:23
  • can you print out the elements of keywords array? Commented Jun 22, 2011 at 7:26
  • Its a big list, but its standard array Commented Jun 22, 2011 at 7:31
  • 1
    This suggests otherwise stackoverflow.com/questions/2010892/… Commented Jun 22, 2011 at 7:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.