Get multi parameter values one-time from chrome.storage.local

1.6k views Asked by At

I wanna inject a content script to pages with a method with two parameters. Since the Chrome.storage.local.get() is asynchronous, I have to make sure that all parameters are initialized from the chrome.local.storage and then begin to invoke my method.

Now I can get only one parameter every time with code like :

var parameter1;
storage.get('parameter1', function(items) {
    parameter1 = items.parameter1;
    if (parameter1) {
        //do sothing
    }
});
var parameter2;
storage.get('parameter2', function(items) {
    parameter2 = items.parameter2;
    if (parameter2) {
        //do sothing
    }
});

But this is not enough to call my method which contains 2 parameters:

function myMethod(parameter1, parameter2);

So how to get and retrieve them ?

2

There are 2 answers

0
Yam Shargil On

If anyone comes back here in 2021 :)

chrome.storage.sync.get(["CONST", "user", "devMode"], (res) => {
    reallyAwesomeFunction(res.CONST.PI, res.user.id, res.devMode);
});
1
pavanw3b On

You can try something like this -

function myMethod(parameter1, parameter2) {
    var details = [parameter1, parameter2];        
    storage.get(details, function(items) {
        alert(items.parameter1 + ' '+items.parameter2);
    });
}