0

Is there a way to set the key for chrome.storage.local.set via a string that is in a variable.

For instance, I want to send a file name and a key name into a single function to store the results into chrome.local.storage.set. It would look something like this.

Call:

setURLS('fileLocation', 'listofurls');
setURLS('secondFileLocation', 'listofsomethingelse');

Method:

function setURLs(fileName, keyName) {
    let textFile = new XMLHttpRequest();
    textFile.open('GET', fileName , true);
    textFile.onreadystatechange = function(){
        if(textFile.readyState === XMLHttpRequest.DONE && textFile.status === 200){
            let dictionaryArray = textFile.responseText.split(',');
            chrome.storage.local.set({keyName: dictionaryArray}, function (){});
        }
    }
    textFile.send(null);
}

It doesn't like to use the string from the variable in the function header. I have to manually set the name of the key which duplicates code for all files that I would like to pull data from.

I hope that I explained it well enough to understand.

kevorski
  • 796
  • 1
  • 7
  • 26

0 Answers0