I can't seem to find out why this does not work from reading the Chrome API Reference so I was wondering if anyone here could tell me why If I save like this:
// grab element and save to storage API
let ELEMENT_DATA = [some string];
chrome.storage.sync.set({"ELEMENT_NAME": ELEMENT_DATA }); //this is the important bit because of the "" around ELEMENT_NAME.
and get like this:
chrome.storage.sync.get(["ELEMENT_NAME"], function (result) {
console.log(result.ELEMENT_NAME.id);
});
There is no problem. Although I need to set and get with a variable name that holds a string like this.
for(let i = 0; i < count; i++){
let ELEMENT_NAME = "ELEMENT" + i; /// = ELEMENT0 and so on...
let ELEMENT_DATA = [some string];
chrome.storage.sync.set({ELEMENT_NAME: ELEMENT_DATA }); //PASSING ELEMENT_NAME and not a direct string "" is the issue
}
So I thought i could solve this if I JSON.stringify the ELEMENT_NAME variable like this:
for(let i = 0; i < count; i++){
let ELEMENT_NAME = "ELEMENT" + i; /// = ELEMENT0 and so on...
let ELEMENT_DATA = [some string];
let ELEMENT_NAME_STR = JSON.stringify(ELEMENT_NAME);
chrome.storage.sync.set({ELEMENT_NAME_STR: ELEMENT_DATA });
}
although this has also proven unsuccessful. What am I missing?
let ELEMENT_NAME, ELEMENT_DATA;
let obj = {};
for (let i = 0; i < 50; i++){
ELEMENT_NAME = "ELEMENT" + i; /// = ELEMENT0 and so on...
ELEMENT_DATA = 'This is the ' + i + 'th elements';
obj[ELEMENT_NAME] = ELEMENT_DATA
}
chrome.storage.session.set(obj, _ => {
chrome.storage.session.get('ELEMENT' + 5, console.log); //retrive one item
chrome.storage.session.get(['ELEMENT' + 1, 'ELEMENT' + 3], console.log) //retrive more item
chrome.storage.session.get(null, console.log) //retrive all item
})