1

I am trying to save to Chrome's synced storage, a dynamically generated JSON object.

Essentially, I am running a query to get all the open tabs:

function getTabs(){
  var activeSession = [];
  //Tab query with no parameters, returns all open tabs. 
  chrome.tabs.query({}, function(tabs){
    //For each tab push to active session array     
    tabs.forEach(function(tab){
      activeSession.push({'title': tab.title, 'url': tab.url});
    });
  });
  return activeSession;
}

I then get a name for this group of tabs from user-input, build a JSON object and save this to Chrome's synced storage:

//Retrieve group name from input field
    var group_name = document.getElementById('file_name').value;

    //Call function to retrieve tabs and build JSON object.
    var returned_tabs = getTabs(); 
    var obj = {[group_name]: returned_tabs};
    console.log(obj);

    chrome.storage.sync.set(obj, function() { 
        chrome.storage.sync.get(function (data) {
            console.log(data);
        });        
    });

The obj variable is essentially the structure I am looking for, and works as expected, as indicated by the console.log(obj). Which outputs like this:

Desired Output

However, when I try to store this in Chrome - it simply saves as the group_name with an empty array. So, if I pull this info back out of storage and log it out, I get:

Actual Output

avidfeed
  • 11
  • 2
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – wOxxOm Mar 10 '18 at 11:52
  • Chrome API is asynchronous. – wOxxOm Mar 10 '18 at 11:52

0 Answers0