admin管理员组

文章数量:1404594

I'm trying to store an object in my chrome extension containing other objects using chrome.storage - but am having trouble initializing it and properly fetching it using chrome.storage.sync.get. I understand that I'm supposed to get objects in the form of chrome.storage.sync.get(key: "value", function(obj) {} - the issue is I'm not sure how to

  • Initialize the object the first time with get
  • Properly update my object with set

I have the following code to create the object and add the data I need.

allData = {};
currentData = {some: "data", goes: "here"};
allData[Object.keys(allData).length] = currentData;

This will correctly give me an object with it's first key (0) set to currentData. (Object: {0: {some: "data", goes: "here"}}) Working as intended, and allData[Object.keys(allData).length] = currentData; will properly push whatever currentData is at the time into my Object later on.

But how do I properly store this permanently in chrome.storage? chrome.storage.sync.get("allData", function(datas) {}) fails to create an empty allData variable, as does allData: {}, allData = {}, and a variety of different things that return either undefined or another error. How do I properly initialize an empty object and store it in chrome.storage? Or am I going about this all wrong and need to break it down into associative arrays in order for it to work?

I essentially need that small block of working code above to be stored permanently with chrome.storage so I can work with it as needed.

I'm trying to store an object in my chrome extension containing other objects using chrome.storage - but am having trouble initializing it and properly fetching it using chrome.storage.sync.get. I understand that I'm supposed to get objects in the form of chrome.storage.sync.get(key: "value", function(obj) {} - the issue is I'm not sure how to

  • Initialize the object the first time with get
  • Properly update my object with set

I have the following code to create the object and add the data I need.

allData = {};
currentData = {some: "data", goes: "here"};
allData[Object.keys(allData).length] = currentData;

This will correctly give me an object with it's first key (0) set to currentData. (Object: {0: {some: "data", goes: "here"}}) Working as intended, and allData[Object.keys(allData).length] = currentData; will properly push whatever currentData is at the time into my Object later on.

But how do I properly store this permanently in chrome.storage? chrome.storage.sync.get("allData", function(datas) {}) fails to create an empty allData variable, as does allData: {}, allData = {}, and a variety of different things that return either undefined or another error. How do I properly initialize an empty object and store it in chrome.storage? Or am I going about this all wrong and need to break it down into associative arrays in order for it to work?

I essentially need that small block of working code above to be stored permanently with chrome.storage so I can work with it as needed.

Share Improve this question asked Jan 21, 2015 at 6:05 ThirkThirk 5931 gold badge7 silver badges24 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You first need to set the data inside the storage:

allData = {};
currentData = {some: "data", goes: "here"};

// to initialize the all data using the storage
chrome.storage.sync.get('allData', function(data) {
  // check if data exists.
  if (data) {
      allData = data;
  } else {
      allData[Object.keys(allData).length] = currentData;
  }
});

// Save it using the Chrome extension storage API.
chrome.storage.sync.set({'allData': allData}, function() {
  // Notify that we saved.
  message('Settings saved');
});

After that you should be able to access the data using the chrome.storage.sync.get('allData', function(){ ... }) interface.

You can easily do this with the new JavaScript (ECMAScript 6), have a look into Enhanced Object Properties:

var currentData = {some: "data", goes: "here"};
chrome.storage.local.set({allData: currentData });

In the old way, was something like this:

var obj = {};
var key = "auth";
obj[key] += "auth";
obj[key] = JSON.stringify({someKey: someValue});

本文标签: javascriptProperly storing an object in chromestorageStack Overflow