1

I'm trying to put a GreaseMonkey script together (with jQuery support) that gathers a little information on a user, that's stored on a subdomain. I finally got it working, after a long while, and rather than 'querying' 60 things each time I refresh the same page (with every page you visit you 'query', on average, another 60 new users), I want to use localStorage to relieve the load on the server (especially as it's not mine. Not planning on making this code public so I should be fine).

Now, I'm using

var current = {};
current.profile = profile;
current.name = $(data).find('.userName').text();
localStorage.setItem('userInfo', JSON.stringify(current));

This simply overwrites the userinfo with the latest profile it queries, let's say profile: 1234 and name: Mave. I want to be able to store new entries as they come, possibly a date so when it's older than 30 days, retrieve them again, and load them from localStorage when they exist.

I've thought of using

current[profile].profile = profile;
current[profile].name= $(data).find('.userName').text();

but it doesn't even work because it doesn't support array fields in that way, I suppose. Any pointers?

Mave
  • 2,413
  • 3
  • 28
  • 54

1 Answers1

1

I think the problem is that you keep over writing userInfo in your localStorage. You should be keying on something unique, like profile.

var current = {};
current.profile = profile;
current.name = $(data).find('.userName').text();
localStorage.setItem(profile, JSON.stringify(current));
Jason
  • 1,879
  • 16
  • 19
  • That would work, but wouldn't that mess up the localStorage? I thought I had to put them in an array to save space, keep the clutter to a minimum? – Mave Dec 24 '13 at 13:45
  • It would take up slightly more space this way, but local storage is local to you. So you do have the max available space to use. See here for limits per browser, http://dev-test.nemikor.com/web-storage/support-test/ – Jason Dec 24 '13 at 13:49
  • Works beautifully - I should have known better haha. What's the best way to add another field to it? Now I've got 1234: Mave stored, but how to add a date field? 1234: Mave, 2013-12-24 so I can check if it's due for a new version? – Mave Dec 24 '13 at 15:43
  • What ever you add to the current object should be stringified and set in local storage. – Jason Dec 24 '13 at 21:42