From what I understood, it is not possible to read files using Greasemonkey. I need to define lot of static configuration files, and it seems completely messy to inc开发者_如何转开发lude those directly in the user script.
So, what is the best way to store and retrieve that data?
Yes, it is not possible, by design, to read local files.
If your files are all a page or so of text, and don't change that often, go ahead and use GM_getResourceText()
, per Dr.Molle's answer.
If your files are large, or change rapidly, then host them on a server and use GM_xmlhttpRequest()
to read back the file you need, at runtime. See item 2, below.
Using
GM_getResourceText()
is the easiest and fastest way to proceed. And, if the config files are reasonably sized (say less than 1K on average), this method will probably be faster than methods that rely on Ajax -- even with 150 (!!!) files.Drawback: You will have to bookmark an awkward path and remember it, when you want to edit a config file. (EG:
C:\Documents and Settings\<Windows login/user name>\Application Data\Mozilla\Firefox\Profiles\<profile folder>\gm_scripts\YOUR_SCRIPT_NAME\
)If you really can't -- or don't want to -- use a bunch of relatively static files, the best choice is to host the config files on your server and have
GM_xmlhttpRequest()
read the needed file when the script runs.If you do not have your own server, then you can host the files someplace like Google Docs.
Using
GM_getValue()
or using globalStorage or local storage can all be very fast and efficient about getting your config file data (and only the data you want). But, storing the data can be a major pain unless you write your own tools to do so (also a pain, but others have done it).Using
@require
offers no advantage overGM_getResourceText()
unless your config files are really valid JavaScript code. And, this method will require you to uninstall and then reinstall your script every time the name of an include file changes -- or if you add or delete files.
Not recommended.
You can load a ressource: http://wiki.greasespot.net/GM_getResourceText
Or you can load it automatically using @require
精彩评论