[Greasemonkey] Greasemonkey Imports

Gareth Andrew freega at freegarethandrew.org
Thu Sep 8 21:55:43 EDT 2005


Hi.

I have added the ability to attach files to a greasemonkey script, so
that when it is installed the files get downloaded and saved with the
script.
I have added a new metadata annotation @import. This annotation has two
parameters, a symbolic name for the import, and a url from which the
import can be downloaded eg.

// ==UserScript==
// @name          MyUserScript
// ...
// @import  importName  http://example.com/path/to/import

The user script can access the import by calling GM_getImport(<import
name>). This function returns a GM_Import object with three accessor
functions.

      * getFileURL() - this method returns a file:/// url (as a string)
        pointing at the imported resource.
      * getContents() - this method returns the file contents (as a
        string).
      * getDataURL() - this method returns the file contents encoded as
        a data URL (as a string). Not yet implemented.

Imports can be any filetype (although there are obvious security
considerations here), so they can be used for embedding
images/multimedia, externalising css and html, or as a means to import
javascript libraries.
Javascript libraries can be injected withe by using eval() which makes
the code immediately available as if it were written inline, or by
inserting the script into the document via the DOM, which is useful for
scripts that are not aware of greasemonkey.

Most of the API's listed on
http://dunck.us/collab/GreaseMonkeyUserScripts#preview do not require
chrome privileges, and could easily be implemented as libraries.  This
combats the potential feature bloat problem that happens when
everybody's favourite function is added to greasemonkey.  It also means
that it is much easier to take advantge of JS libraries not written for
greasemonkey such as. MochaKit, TrimPath, Prototype etc.

Please post your feedback, and if you're very brave try out the code
(based on 0.6.x - so I don't know if it works on 1.0.x).  More
information available at
http://www.freegarethandrew.org/mediawiki/index.php?title=GreasemonkeyImports

Gareth.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: greasemonkey.xpi
Type: application/zip
Size: 71560 bytes
Desc: not available
Url : http://mozdev.org/pipermail/greasemonkey/attachments/20050908/bd22c1a3/greasemonkey-0001.zip
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ImportsTest.user.js
Type: application/x-javascript
Size: 1261 bytes
Desc: not available
Url : http://mozdev.org/pipermail/greasemonkey/attachments/20050908/bd22c1a3/ImportsTest.user-0001.js


More information about the Greasemonkey mailing list