2

My script adds some annotations to each page on a site, and it needs a few MBs of static JSON data to know what kind of annotations to put where.

Right now I'm including it with just var data = { ... } as part of the script but that's really awkward to maintain and edit.

Are there any better ways to do it?

taw
  • 18,110
  • 15
  • 57
  • 76
  • Are there many pages or sites, or just a site? Maybe including a sample of the data which could help us look into it better? – mfirdaus May 20 '14 at 13:39
  • It's one static JSON nested hash, from which a script decides to annotate various things on (potentially) all pages on a single website. Everything is really simple except for size of that hash. – taw May 21 '14 at 14:07

2 Answers2

2

I can only think of two choices:

  1. Keep it embedded in your script, but to keep maintainable(few megabytes means your editor might not like it much), you put it in another file. And add a compilation step to your workflow to concatenate it. Since you are adding a compilation you can also uglify your script so it might be slightly faster to download for the first time.

  2. Get it dynamically using jsonp. Put it on your webserver, amazon s3 or even better, a CDN. Make sure it will be server cachable and gzipped so it won't slow down the client network by getting downloaded on every page! This solution will work better if you want to update your data regularly, but not your script(I think tampermonkey doesn't support auto updates).

Community
  • 1
  • 1
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
2

My bet would would definetly be to use special storage functions provided by tampermonkey: GM_getValue, GM_setValue, GM_deleteValue. You can store your objects there as long as needed.

Just download the data from your server once at the first run. If its just for your own use - you can even simply insert all the data directly to a variable from console or use temporary textarea, and have script save that value by GM_setValue.

This way you can even optimize the speed of your script by having unrelated objects stored in different GM variables.

AlexTR
  • 772
  • 8
  • 14