PDA

View Full Version : [Java] How to use Google Chrome's .storage API?



CaptainNight
11-19-2014, 10:33 AM
So uh, im trying to make a google chrome extension that I eventually plan on sharing with this forum. This is literally my first ever coding project besides "Hello World" stuff in a highschool class several years ago. So excuse how noob I am.

All im trying to do is make a semi-legit RS Chrome extension that greys out junk items and highlights UBs.

Right now im just abusing IF loops and have all these shop lists directly in the content_script. So blah blah IF =shop&obj_type=30 THEN it sets var Junk to be Spooky Foods junk list and var UB to a Spooky Foods UB list.

Then I have it where it simply iterates all the image URLs through the var Junk and greys out all the Junk images, then iterates through UB list and expands those images. That leaves the junk items greyed out, UBs highly visible, and the other profitable items (like 5k-99k) normal.

This is functional right now and does exactly what I want it to do for the shops I have loaded. HOWEVER, the lists are typed directly into the content script and immutable :/ If I want to add or remove items I have to physically open up my extension files and its really annoying to have to dredge through the wall of image URLs to add to them. Not to mention that I have to repack the extension every time.

Ideally I would have something like in adblock where I can right click and add/remove to Junk/UB list for that particular shop, and also have some way to load text files provided by the user. Its also stupidly inefficient right now since it iterates through the whole list for every image, but that is not too bad. Mostly I want to have the read to and from storage functionality and I can figure out the rest from there. Iv found decent tutorials for the other stuff.

I can't for the life of me figure out how to host the lists NOT directly within the content_script. I would like to be able to edit the lists individually. I tried mucking around in adblockplus's source code for help but I can't actually find the functions that write to lists? Obviously it stores the whitelists/blacklists somewhere. The chrome documentation talks about the storage API but it doesn't tell me how to actually use it..

If someone could point me in the right direction to a noob tutorial that would be nice. Or provide a very simple example of using the API to store something in a list and then how to access that list?

It looks like this ([Only registered and activated users can see links]) right now and is kinda shitty but it actually REALLY helps in restocking semi-legit. Its like training wheels. Even if no one else ever uses it I think its a good project for myself.

j03
11-19-2014, 11:47 AM
This is looking really good and with the new restock system in place, it will definitely have some interest from other members of the forum. Unfortunately, I do not have experience developing chrome extensions. Maybe someone else can help..

reestoquer
11-19-2014, 12:11 PM
Wow nice job. You intends to make available in this way for other users to work upon?

CaptainNight
11-19-2014, 03:59 PM
lol how helpful Joe ;P

I had hoped someone on this forum would be able to help but I guess the next step is go to a Chrome extension dev forum or something and hope they don't laugh at me too much for making a neopets tool.

I could also just do it in firefox but from what I see, its mostly the same. It has a strange storage API. Plus I personally use Chrome and im not wasting my time making a tool for a browser that I dislike :P Especially since I am just now learning to code and every small thing takes forever. Baby steps. When its finished ill try to port it though since I know lots of people use that... or ill give it to someone who can port it for me.

I do plan on putting it on clraik. I don't know the procedure for that but yeah. I think its something we "need" around here. Most people who semi-legit restock seem to use the adblock lists, which are kinda clunky. And there is no way to make it only block the images on the shop page and not say, in your inventory or in the trades. Plus sometimes I want to buy unprofitable items and not have all the images hidden... just greyed out.

ANYWAY.

Ill figure this out eventually.

EDIT:

So if no one actively KNOWS chrome, do any of you think you could de-jargon the documentation? It doesn't look very long. Sorry I am a noob.

[Only registered and activated users can see links] - basic overview of chrome extensions themselves

[Only registered and activated users can see links] - storage API stuff

[Only registered and activated users can see links]

[Only registered and activated users can see links] - This is a question someone else asked that I think I understand the API itself, but still doesn't help with storing a list.

It assumes I already know some things that I do not know. I can see how to store a value, but how do I store and edit a list in the API? I don't know how to compare the individual items/images I have to the stored values to actually check if its in the storage. If that even makes sense. The examples look like storing settings and individual bits of info.

j03
11-19-2014, 05:01 PM
Maybe post some code snippets and I might be able to help. Working from one language to another isn't very hard. :)

AliasXNeo
11-19-2014, 09:24 PM
You might want to correct your title, Chrome uses a subset of Javascript for extensions (not Java which is a completely different and unrelated language).

As far as the API goes:


function saveChanges() {
// Get a value saved in a form.
var theValue = textarea.value;
// Check that there's some code there.
if (!theValue) {
message('Error: No value specified');
return;
}
// Save it using the Chrome extension storage API.
chrome.storage.sync.set({'value': theValue}, function() {
// Notify that we saved.
message('Settings saved');
});
}

The above code is taking the content of a TextArea HTML element (which is assumed to exist as part of a form) and saving it to Chrome's internal storage. In the above case, they are using the Sync API which stores the information in the cloud and travels with the user.

For saving, they are passing key/value pairs along with a callback function. It looks like the keys are string values used for identifying stored information and the values can be basically anything that can be serialized by JSON. So, if you're storing a list of items, I'd imagine you'd want to do this:


function saveItems() {
// The list of items
items = ['Carrot', 'Yellow Snowball', 'Poop']
// Save them
chrome.storage.sync.set({'General Shop': items});
}

And to retrieve it later:


function getItems() {
// Get the items and do something....
items = chrome.storage.sync.get('General Shop')
}

However, as far as I can tell you won't be able to edit these values outside of your extension (as they will be stored in the cloud). If you go this route you'd need to code a user interface for editing your saved lists (which wouldn't be too difficult).

CaptainNight
11-19-2014, 10:21 PM
Ahhhhhhh so I can just save the list under some name??? Like 'General Shop Junk'?? I kept trying to store the list itself as a var in the local part of the API and it wouldn't work and I was getting frustrated. Haha.

I can easily just call the function and assign the list to the var junk/UB im already using. Perfect.

I had already planned on making some kind of interface for adding in lists. In the last few hours I found some good info on the GUIs that I can adapt to this really well. For now I can at least have it load some "default" lists on the first run time I think.

Thank you so much. Iv only used python prior to this and it was years ago so a lot of this looks like moon runes to me. I want to rep you 20 times.

If I hit another wall, ill be back :P

AliasXNeo
11-19-2014, 10:34 PM
Ahhhhhhh so I can just save the list under some name??? Like 'General Shop Junk'?? I kept trying to store the list itself as a var in the local part of the API and it wouldn't work and I was getting frustrated. Haha.

I can easily just call the function and assign the list to the var junk/UB im already using. Perfect.

I had already planned on making some kind of interface for adding in lists. In the last few hours I found some good info on the GUIs that I can adapt to this really well. For now I can at least have it load some "default" lists on the first run time I think.

Thank you so much. Iv only used python prior to this and it was years ago so a lot of this looks like moon runes to me. I want to rep you 20 times.

If I hit another wall, ill be back :P

No worries. It might be worth your time to go through something like CodeAcademy's Javascript Tutorial ([Only registered and activated users can see links]) to get yourself familiar with the syntax. I don't recall exactly what Chrome uses for extensions (I think it's related to Google Apps Script), but it's really nothing more than a sandboxed version of javascript (meaning certain functionalities removed for the safety of the end-user).