how to access multiple keys from chrome extension's storage api

2,125 views
Skip to first unread message

Ratan Paul

unread,
Oct 27, 2016, 9:13:12 AM10/27/16
to Chromium-Extensions-Announce
Hey Guys, 

I have 2 input textbox for username and password. And one picklist for some value. Now I want a user to add these values and I am trying to save those values using chrome storage API. 

 Now each time I am storing a variable as how many records added like counter. 
 for storing I am just using like this

var skillsSelect = document.getElementById("orgTypeId");
var selectedOrgType = skillsSelect.options[skillsSelect.selectedIndex].value;
var countTotalRecords = totalRecordsVal +1;
var username = 'username'+countTotalRecords;
var password = 'password'+countTotalRecords;
var orgType = 'orgType'+countTotalRecords;

        chrome.storage.sync.set({
        username: $("#usernameid").val(), 
        password: $("#passwordid").val(),
        orgType: selectedOrgType,
        'totalRecords':countTotalRecords
        }, function(){
        alert('Success!');
        });


AND for retrieving I am trying like this way 

var getItems = [];
for(var i = 1; i <10; i++){
var username  = 'username'+i;
var password  = 'password'+i;
var orgType  = 'orgType'+i;
getItems.push(username);
getItems.push(password);
getItems.push(orgType);
}
console.log('==========getItems======',getItems);
chrome.storage.sync.get(getItems, function(items){
var keys = Object.keys(items);
for (var i = 0, end = keys.length; i < end; i++) {
var key = keys[i];
console.debug(key + ' = ' + items[key]);
}
        console.log('------------data---',data);
       
        });


But I am not getting all the stored values. even I am not getting all the values in console log. 

PhistucK

unread,
Oct 27, 2016, 9:29:28 AM10/27/16
to Ratan Paul, Chromium-Extensions-Announce
I believe this is a basic JavaScript language misunderstanding. stackoverflow.com would be have been more appropriate for this question, really.

In the following code (your code, plus my comments), username is literally the name of the key, it is not replaced with the content of the username variable (quotes are optional in simple key names in object literals).
chrome.storage.sync.set({
         username: $("#usernameid").val(), // A key named "username", not "username0"
         password: $("#passwordid").val(), // A key named "password", not "password0"
         orgType: selectedOrgType, // A key named "orgType", not "orgType0"
         'totalRecords':countTotalRecords
         }, function(){
         alert('Success!');
        });

In ECMAScript 2015 (ES6), which is supported by Chrome, you can do what you meant like this -
chrome.storage.sync.set({
         [username]: $("#usernameid").val(), // A key named by the value of the username variable
         [password]: $("#passwordid").val(), // A key named by the value of the password variable
         [orgType]: selectedOrgType, // A key named by the value of the orgType variable
         'totalRecords':countTotalRecords
         }, function(){
         alert('Success!');
        });




PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/77071d11-e28b-4994-843f-60b1eb354ef2%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Reply all
Reply to author
Forward
0 new messages