سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top