【问题标题】:Getting Multiple items from Chrome storage?从 Chrome 存储中获取多个项目?
【发布时间】:2020-10-22 13:05:39
【问题描述】:

我有 4 件物品想要获得,但我不确定如何分开钥匙。使用逗号会出错。这是我的用法示例:

chrome.storage.sync.get({
    'customImage',
    'customColor',
    'customRandColor',
    'customRandImage'
  }, function(backgroundCheckedOptions) {
    document.getElementById('optionsCustomImage').checked = backgroundCheckedOptions.customImage;
    document.getElementById('optionsBackgroundColor').checked = backgroundCheckedOptions.customColor;
    document.getElementById('optionsRandomColor').checked = backgroundCheckedOptions.customRandColor;
    document.getElementById('optionsRandomImage').checked = backgroundCheckedOptions.customRandImage;
  });

我原以为它们会用逗号分隔,但我想不会。

【问题讨论】:

    标签: google-chrome-extension google-chrome-storage


    【解决方案1】:

    来自Chrome Storage documentation,它说:

    StorageArea.get(字符串或字符串或对象键的数组,函数回调)

    最简单的方法是通过将 {} 替换为 [] 来传递数组

    【讨论】:

    • 对象形式也可用于提供默认值:{key1: default1, key2: default2}
    【解决方案2】:

    根据官方documentation,如下。

    chrome.storage.sync.get([
     'customImage',
     'customColor',
     'customRandColor',
     'customRandImage'
    ], function(backgroundCheckedOptions) {
     document.getElementById('optionsCustomImage').checked = backgroundCheckedOptions.customImage;
     document.getElementById('optionsBackgroundColor').checked = backgroundCheckedOptions.customColor;
     document.getElementById('optionsRandomColor').checked = backgroundCheckedOptions.customRandColor;
     document.getElementById('optionsRandomImage').checked = backgroundCheckedOptions.customRandImage;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-25
      • 2016-06-02
      相关资源
      最近更新 更多