简体   繁体   English

如何使用tampermonkey脚本附加大量数据?

[英]How to attach large amounts of data with tampermonkey script?

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. 我的脚本向站点的每个页面添加了一些注释,并且它需要几MB的静态JSON数据才能知道将哪种注释放置在何处。

Right now I'm including it with just var data = { ... } as part of the script but that's really awkward to maintain and edit. 现在,我只将var data = { ... }作为脚本的一部分包括在内,但是维护和编辑确实很尴尬。

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 . 使用jsonp动态获取。 Put it on your webserver, amazon s3 or even better, a CDN. 将其放在您的Web服务器(Amazon s3甚至更好的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). 如果您想定期更新数据,而不是脚本,则此解决方案将更好地工作(我认为tampermonkey不支持自动更新)。

My bet would would definetly be to use special storage functions provided by tampermonkey: GM_getValue , GM_setValue , GM_deleteValue . 我的赌注肯定是使用tampermonkey提供的特殊存储功能: GM_getValueGM_setValueGM_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 . 如果只是供您自己使用-您甚至可以直接从控制台将所有数据直接插入到变量中,或使用临时textarea,然后让脚本通过GM_setValue保存该值。

This way you can even optimize the speed of your script by having unrelated objects stored in different GM variables. 这样,您甚至可以通过将不相关的对象存储在不同的GM变量中来优化脚本的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM