简体   繁体   中英

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

I am making a chrome extension that does two different things depending on the option set by the user. How do I call the value stored in chrome storage

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

from a different js file? For example:

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

You just have to call the get method with the key you used to store the data, in this case selectedChoice and use a callback function to execute once the value is retrieved. Example:

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

More herehttps://developer.chrome.com/docs/extensions/reference/storage/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM