尝试使用chrome.storage API保存并获取Javascript对象? [英] Trying to save and fetch a Javascript object using chrome.storage API?

查看:222
本文介绍了尝试使用chrome.storage API保存并获取Javascript对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个Chrome扩展程序,并且希望它能跟踪我观看的电视剧,并且我正在试图将它保存在我关注的系列中的元数据。

I'm building my first chrome extension and I want it to track the TV series I watch and I'm currently trying to get it to save metadata on the series that I am following.

我有一个内容脚本,可以返回标题,最新剧集(以及本集的URL)以及系列封面图片的URL。我目前正试图用我的后台脚本中的一些代码保存它(我已经确保在清单文件的权限部分包含存储)。

I have a content script that returns the title, the newest episode (and the URL of this episode) as well as the URL of the cover image of the series. I am currently trying to save it with some code on my background script (I have made sure to include "storage" under the permissions section of the manifest file).

从我的应用程序需要能够跟踪多个系列,我创建了一个对象构造函数,用于存储使用对象获取的元数据:

Since my app needs to be able to track more than one series, I made an object constructor that stores the metadata I fetch using an object:

function Series(nTitle, nNewEp, nNewEpURL, nImage) {
    this.anTitle = nTitle;
    this.anNewEp = nNewEp;
    this.anNewEpURL = nNewEpURL;
    this.anImage = nImage;
}

然后尝试通过以下代码保存这些数据:

I then attempt to save this data via the following code:

var bkg = chrome.extension.getBackgroundPage();
response.aID = new Series(response.aTitle,response.aNewEp,response.aNewEpURL,response.aImage);

                chrome.storage.sync.set(response.aID, function(){
                    chrome.storage.sync.get(response.aID.anTitle, function(val){
                        bkg.console.log("The saved title is: ", val);
                    });
                    chrome.storage.sync.get(response.aID.anNewEp, function(val){
                        bkg.console.log("The saved newEp is: ", val);
                    });
                    chrome.storage.sync.get(response.aID.anNewEpURL, function(val){
                        bkg.console.log("The saved newEpURL is: ", val);
                    });
                    chrome.storage.sync.get(response.aID.anImage, function(val){
                        bkg.console.log("The saved imageURL is: ", val);
                    });
                });

我的回调函数用于检查过程并打印出我保存的值。但是我注意到,我返回的每个值都只是一个空的JQuery包装器。我曾尝试使用val [0]而不是val,但返回undefined。这让我觉得我没有正确保存我的对象,或者Chrome不会将 response.aID 识别为对象。所以我的问题是我该如何做这样的工作?

My callback function was designed to check the process and print out the values that I saved. However I have noticed that every value I return is simply an empty JQuery wrapper. I have tried using val[0] instead of val, but that returns "undefined". That makes me think that I am not saving my object properly or that Chrome does not recognise response.aID as an object. So my question is how can I make something like this work?

推荐答案

您的问题在回调中:

当您试图获取 response.aID.anTitle 时,您实际上正在查找响应中设置的字符串.aID.anTitle 即`我的真棒肥皂。

When you are trying to get response.aID.anTitle, your actually looking for the string set in response.aID.anTitle i.e `"my Awesome Soap".

您想要的是获取保存对象的anTitle键。

What you want is to get "anTitle" key of the saved object.

一种方法是仅获取整个storageArea对象,并只记录您想要的键:

One way is to only get the whole storageArea object and just log your wanted keys :

 chrome.storage.sync.set(response.aID, function(){
      chrome.storage.sync.get(function(val){
          bkg.console.log("The saved title is: ", val.anTitle);
          bkg.console.log("The saved newEp is: ", val.anNewEp);
          bkg.console.log("The saved newEpURL is: ", val.anNewEpURL);
          bkg.console.log("The saved imageURL is: ", val.anImage);
      });

,或者如果你真的想打电话给 get()

or if you really want to make so much call to get():

   chrome.storage.sync.set(response.aID, function(){
        chrome.storage.sync.get('anTitle', function(val){
          bkg.console.log("The saved title is: ", val);
          });
        chrome.storage.sync.get('anNewEp', function(val){
          bkg.console.log("The saved newEp is: ", val);
          });
        chrome.storage.sync.get('anNewEpURL', function(val){
          bkg.console.log("The saved newEpURL is: ", val);
          });
        chrome.storage.sync.get('anImage', function(val){
          bkg.console.log("The saved imageURL is: ", val);
          });
      });

但是这个会返回一个只包含你的键和它的值的对象,所以如果你需要你的价值,即 console.log(val.anTitle)

But this one will return an object, containing only your key and its value, so if you need the value you've got to i.e console.log(val.anTitle)

这篇关于尝试使用chrome.storage API保存并获取Javascript对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆