javascript - How to save multiple objects to an array in a chrome extension? -
i'm building first chrome extension , want track tv series watch , i'm trying save metadata on series following.
i have content script returns title, newest episode (and url of episode) url of cover image of series. trying save code on background script (i have made sure include "storage" under permissions section of manifest file).
so far script looks (this developed trying save , fetch javascript object using chrome.storage api?):
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(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); }); });
problem is, script seems store 1 response.aid
@ time, can never store data more 1 tv series. every time try, script seems overwrite previous entry. ask whether there's way store more 1 tv series @ time?
i have looked @ storing array , pushing each new object array (store array chrome.storage.local), don't quite understand syntax involved i'm not sure if work me.
unfortunately didn't include piece of code save data, think dont store data indices different tv series stored 1 gets overwritten everytime store one.
anyway prefer storing data in json element (basically every javascript element can converted 1 continue reading) because js provides several functions format make quite easy use.
when opening extension, load data , call
var data = json.parse (yourloadedstring);
so string (which should {"tvshows": [{"title": "how met mother", "url": ...}, {...}]}
(look here explenation how json works) gets "translated" element can read calling
data.tvshows[0].title
or
data.tvshows[1].imageurl
you can edit data
json element when add new show example saying
data.tvshows[2].title = "the big bang theory"; data.tvshows[2].url= ...; data.tvshows[2].imageurl= ...;
and save element chromes storage calling
var datatosave = json.stringify(data);
you have string in storage then, containing information need , can parse later explained above :)
i hope understand, if not pls ask me!
cheers
Comments
Post a Comment