我想通过 chrome.storage.local.get() 从 chrome 存储中获取 2 个值,但是如果我尝试从回调函数返回值,我会得到“未定义”。如果我 console.log() 它们,它们实际上已被记录。这是为什么?我在其他一些帖子中读到过,我需要使用回调 - 但就我对 JS 的基本理解而言,我已经从 get() 的回调函数返回了值。
// this should load 2 keys from an object stored in chrome storage
function loadJiraUrlElements(){
chrome.storage.local.get({companyName: '', jiraBaseUrl: ''}, function(items) {
return (items.companyName + items.jiraBaseUrl);
})
}
// I want to use this function to call the return value and populate a DOM element
function populateUrlFieldsWithUrlElements(){
companyInput.value = loadJiraUrlElements()
}
Run Code Online (Sandbox Code Playgroud)