如何从单独的 js 文件中调用存储在 chrome.storage 中的变量值?

How do I call a variable value stored in chrome.storage from a separate js file?

我正在制作一个 chrome 扩展,它根据用户设置的选项做两件不同的事情。如何调用存储在 chrome storage

中的值
// Saves options to chrome.storage
function save_options() {
    var choice = document.getElementById('choices').value;
    chrome.storage.sync.set({
        selectedChoice: choice
    });
}

来自不同的 js 文件?例如:

function loadOptions() {
    chrome.storage.sync.get({
        theVariablePreviouslyStored
})
}

您只需使用用于存储数据的键调用 get 方法,在本例中为 selectedChoice,并在检索到值后使用回调函数执行。示例:

function loadOptions() {
    chrome.storage.sync.get(['selectedChoice'], function(result) {
  console.log('Value currently is ' + result.key);
});
}

这里有更多内容https://developer.chrome.com/docs/extensions/reference/storage/