0

I'm having trouble using the chrome.storage.local.get feature. I have all working, except I don't understand how to handle the .get function. I want to copy an object by clicking a button, from one chrome tab to another one. I have a loop to get my Object keys, and I can save it successfuly in chrome local storage by:

chrome.storage.local.set({
    'copiedObj': obj
});

but, when trying to use the .get function, I can't specify the key number I want. I need the key number because the keys and their values changes every time because i'm copying data from different pages.

$('#targetInfo').click(function(){
   console.log('context 2 received');

    var storage = chrome.storage.local.get('copiedObj', function (items) {
    console.log(items); 
    });

});

this gets me the full object keys and values, but when trying to change it to get the first key (changing the console.log inside the script to items[0], I get an undefined error message in the console.

I want it to iterate through all the keys, and assign key to a variable, value to a variable and execute a function. For each key by it's time. I've written a detailed explanation inside the code:

//Get the DOM input field
var specific_name = document.getElementById('new_specific_name');
var specific_value = document.getElementById('new_specific_value');

//Start loop, from 0 to the last object keys
for (i=0;i<items.length;i++) {

    //Set specific_name value to the first key inside the object
    specific_name.value = XXX[i];

    //Set specific_value value to the matching key value inside the object
    specific_value.value = ZZZ[i];

    //Execute this function
    add_new_specific();
}
serv-inc
  • 35,772
  • 9
  • 166
  • 188
Asaf Kfir
  • 129
  • 2
  • 9
  • 2
    1. chrome.* API is asynchronous so the value can be used only inside the callback. 2. your object is in `items.copiedObj` – wOxxOm Apr 22 '17 at 14:41
  • OK, but how can I access a specific key, or key's value? the first key, or the second key for example? The object value has multiple keys – Asaf Kfir Apr 22 '17 at 14:51
  • 2
    Just like any other object in JavaScript. – wOxxOm Apr 22 '17 at 14:52
  • Please [edit] the question to be on-topic: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this almost always means including your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Apr 24 '17 at 01:17
  • Do you want to iterate through all objects in storage, or do you want to get one object, and iterate through all its keys? – serv-inc Apr 27 '17 at 21:10

1 Answers1

0

(minimal solution, pending clarification) As mentioned before,

var storage = chrome.storage.local.get('copiedObj', function (items) {
    console.log(items); 
});

should be

var storage = chrome.storage.local.get('copiedObj', function (result) {
    console.log(result.items);
    console.log(result.items[0]); // if items is an array...
});

To loop through an object, see https://stackoverflow.com/a/18202926/1587329. You need to do this all inside the anonymous function in .get.

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188