Google chrome extension 通过指定要获取的对象属性,最小化用于chrome存储的流量

Google chrome extension 通过指定要获取的对象属性,最小化用于chrome存储的流量,google-chrome-extension,google-chrome-storage,Google Chrome Extension,Google Chrome Storage,在我的扩展中,我将某些域的设置设置为以下格式: { domainSettings: { google.com.au: { setting: "someSetting", anotherSetting: "somethingElse" }, stackoverflow.com: { setting: "someOtherSetting" anotherSetting: "somethingElseAswell" }

在我的扩展中,我将某些域的设置设置为以下格式:

{
  domainSettings: {
    google.com.au: {
      setting: "someSetting",
      anotherSetting: "somethingElse"
    },
    stackoverflow.com: {
      setting: "someOtherSetting"
      anotherSetting: "somethingElseAswell"
    }
  }
  otherSettings: {
    ...
  }
}
与其使用
chrome.storage.local.get(“domainSettings”,function(response){})
来获取每个域的设置,然后只获取我需要的设置,我如何才能只获取
google.com.au
的设置

似乎没有必要获得成百上千倍的信息,而我只需要一个


干杯。

你可以用一个合适的数据库来代替,就像暴露在Chrome扩展中一样

或者,您可以从层次结构中删除“domainSettings”,只使用主机名作为键。这完全没有什么错:

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]);
  });
}

最简单的改变是使用更平坦的数据结构

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

Errr@abraham这是什么语法?它不是有效的JSON或JavaScript。@Xan它只是没有被引用