0

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.

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).

So far my script looks like this (This was developed with help from Trying to save and fetch a 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, the script only seems to store one response.aID at a time, so I can never store data for more than 1 TV series. Every time I try, the script seems to overwrite my previous entry. So I would like to ask whether there's any way to store more than 1 TV series at a time?

I have looked at storing an array and then pushing each new object into that array (Store an array with chrome.storage.local), but I don't quite understand the syntax involved so I'm not sure if this would work for me.

Community
  • 1
  • 1

1 Answers1

0

Unfortunately you didn't include the piece of code where you save your data, but i think you dont store your data with indices for the different TV series so the stored one gets overwritten everytime you store another one.

Anyway I would prefer storing your data in a JSON element (basically every javascript element can by converted to one but continue reading) because js provides several functions for this format which make it quite easy to use.

When opening your extension, load the data and call

var data = JSON.parse (yourloadedstring);

so the string (which should look like {"TVShows": [{"title": "How i met your mother", "url": ...}, {...}]} (look here for an explenation how JSON works) gets "translated" to an element from which you can read simply by calling

data.TVShows[0].title

or

data.TVShows[1].imageURL

You can edit this data JSON element when you add a new show for example by saying

data.TVShows[2].title = "The Big Bang Theory";
data.TVShows[2].URL= ...;
data.TVShows[2].imageURL= ...;

and save this element to chromes storage by calling

var dataToSave = JSON.stringify(data);

You have a string in your storage then, containing all information you need and you can simply parse it later like explained above :)

I hope everything is clearly to understand, if not pls ask me!
Cheers

Tom Doodler
  • 1,471
  • 2
  • 15
  • 41
  • Thank you for your reply, but I'm not too sure how to implement your solution, because I want to be able to add TV shows at the click of a button by using the metadata variables supplied by my content script. Also I don't understand how you'd save the `dataToSave` variable because `chrome.storage.sync.set()` does not accept variables as an input, only objects. – GaleHalcyon Aug 19 '15 at 14:35
  • Oh Im sorry then, I don't know the api :/ But arent you able so just save the object like in my description without `JSON.parse(...)` and `JSON.stringify(...)`? – Tom Doodler Aug 19 '15 at 14:38
  • Yeah I've been trying that, but without some sort of index for each entry, they just keep overwriting each other. Thanks anyway, you've taught me a lot about JSON! – GaleHalcyon Aug 19 '15 at 14:44
  • JSON is awesome and simple for storing info, keep it in mind ;) – Tom Doodler Aug 19 '15 at 14:46