GusGold
GusGold

Reputation: 580

Minimalise the traffic used for chrome storage by specifying object properties to get

In my extension, I have settings for certain domains set to chrome.storage.local in the following format:

{
  domainSettings: {
    google.com.au: {
      setting: "someSetting",
      anotherSetting: "somethingElse"
    },
    stackoverflow.com: {
      setting: "someOtherSetting"
      anotherSetting: "somethingElseAswell"
    }
  }
  otherSettings: {
    ...
  }
}

Rather than using chrome.storage.local.get("domainSettings", function(response){}) to get every single domain's setting, and then just getting the one I need, how can I get just google.com.au's settings.

It seems unnecessary to get hundreds or thousands of times the information, when all I need is just one.

Cheers.

Upvotes: 1

Views: 51

Answers (2)

abraham
abraham

Reputation: 47923

The simplist change would be to use a flatter datastucture.

{
  "domainSettings:google.com.au": {
    setting: "someSetting",
    anotherSetting: "somethingElse"
  },
  "domainSettings:stackoverflow.com": {
    setting: "someOtherSetting"
    anotherSetting: "somethingElseAswell"
  },
  ...
}

Upvotes: 1

Xan
Xan

Reputation: 77591

You could always use a proper database instead, as IndexedDB is exposed to Chrome Extensions.

Alternatively, you could drop "domainSettings" from hierarchy and just use hostnames as keys. There's strictly nothing wrong with this:

function set(hostname, options, callback){
  var data = {};
  data[hostname] = options;
  // Should be no need for deep copy, as chrome.storage will serialize
  chrome.storage.local.set(data, callback);
}

function get(hostname, callback){
  chrome.storage.local.get(hostname, function(result){
    callback(result[hostname]);
  });
}

Upvotes: 0

Related Questions