Chuck Baker
Chuck Baker

Reputation: 382

Using chrome.storage.sync to save/get nested objects

I am trying to save/get an object in chrome. The object is nested inasmuch as a property of the object is, itself, another object.

tag.addEventListener('focus', function(event){
var focused = {};
focused.item = event.target;
console.log("Saving",focused) // 'Saving Object {item: input#un2}' where 'item' is a complete object with all event properties listed
chrome.storage.sync.set(focused, function(){
    chrome.storage.sync.get("focused", function(obj){
        console.log("Retrieved focused",obj)    // 'Retrieved focused Object {}' - 'obj' is an empty object
    });
});

});

I display the object just prior to saving and it looks fine. I can see all the properties and values. But when I try to get the object out of storage, it returns an empty object.

I've looked at other similar questions here on SO, but none seem to answer this particular issue.

What I'm trying to do is save information about a HTML tag that gets focus so it is available to another script (in a browser action popup).

Upvotes: 1

Views: 786

Answers (1)

Chuck Baker
Chuck Baker

Reputation: 382

Nevermind ... It would appear that you cannot save objects that contain functions to storage. Booleans, strings, numbers and key/value objects are ok, but not functions.

I was trying to save event.target which contains many functions. It would be nice if storage just strips the functions out and saves the rest, but it seems if a function exists the whole object is turned into an empty object ('{}').

Upvotes: 2

Related Questions