从 Chrome 存储中获取多个项目?

Getting Multiple items from Chrome storage?

我有 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;
  });

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

来自Chrome Storage documentation,它说:

StorageArea.get(string or array of string or object keys, function callback)

最简单的方法是通过将 {} 替换为 []

来传递数组

根据官方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;
});