简体   繁体   English

使用chrome.storage.sync保存/获取嵌套对象

[英]Using chrome.storage.sync to save/get nested objects

I am trying to save/get an object in chrome. 我正在尝试在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. 我在SO上看过其他类似的问题,但似乎没有人回答这个特定问题。

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). 我想做的是保存有关获得焦点的HTML标记的信息,以便其他脚本可以使用它(在浏览器操作弹出窗口中)。

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. 我试图保存包含许多功能的event.target 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 (' {} '). 如果存储只是剥离功能并保存其余功能,那将是很好的选择,但似乎如果功能存在,则整个对象都将变成一个空对象(' {} ')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM