chrome.storage.sync.get Alert not working

chrome.storage.sync.get Alert not working

本文关键字:not working Alert get storage sync chrome      更新时间:2023-09-26

我已经保存了这个设置:

function save_options() {
  var tbColor = document.getElementById('Color').value;
  chrome.storage.sync.set({
    'tbColor': Color
  }, function() {
    var status = document.getElementById('status');
    status.textContent = 'Settings were saved.';
    setTimeout(function() {
      status.textContent = '';
    }, 750);
  });
}

现在我试着通过

查看设置

alert(chrome.storage.sync.get({'tbColor'}));

但是我什么也没得到(没有警报显示)。我知道整体的东西是工作的,因为alert("Hello");工作。

我得到这些设置做错了什么?我是新来的,所以请对我宽容点:)

{'tbColor'}语法无效,因此首先出错。

另外,chrome.storage.sync.get是异步的,所以你不能使用返回值。

你可以使用:

chrome.storage.sync.get("tbColor", function(sync) {
  alert(sync.tbColor);
});