Greasemonkey Manual:Editing: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (→‎Picking an Editor: Shortened table header descrips)
m (→‎User Script Flow: Partial refactor of this header. (aside from cosmetic placement... refactoring is grammar fixes, elimination of "you, I, we, let's, etc" and general reformatting))
Line 4: Line 4:
== User Script Flow ==
== User Script Flow ==


First: a [[user script]] is only a plain text file with the extension <code>.user.js</code>.
A [[user script]] is a plain text file with a file extension of <code>.user.js</code>.
Usually, a [[metadata block]] is included, although this is not strictly required.
Typically a [[metadata block]] is included, although this is not strictly required but is highly recommended.


You may create a [[user script]] with any text editor.
Creation of a [[user script]] can be done with any plain text editor. Upon completion of a script it may be distributed via any server method available or even a specialized user script hosting site such as [http://userscripts.org userscripts.org].
When you're done, distribute it via whatever means you like.
For example, you may place it on a web server.


The tricky part of course is what happens between when you start and when the script is finished.
The tricky part of course is what happens between starting a script and when the script is finished.
Let's walk through all the steps, manually, to make sure we understand what is going on.
Manually walking through all the steps will ensure common understanding of what is going on.


== Creating the Script ==
== Creating the Script ==

Revision as of 06:17, 16 April 2009


Greasemonkey Manual
Using Greasemonkey
Installing Scripts
Monkey Menu
Getting Help
User Script Authoring
Editing
Environment
API


User Script Flow

A user script is a plain text file with a file extension of .user.js. Typically a metadata block is included, although this is not strictly required but is highly recommended.

Creation of a user script can be done with any plain text editor. Upon completion of a script it may be distributed via any server method available or even a specialized user script hosting site such as userscripts.org.

The tricky part of course is what happens between starting a script and when the script is finished. Manually walking through all the steps will ensure common understanding of what is going on.

Creating the Script

First, create the text file. It should contain the metadata block with name, namespace, and any appropriate include and exclude rules and nothing more. At this point, you may begin with any of the script itself, but it is not yet necessary, and likely is counterproductive. Now, install this stub script into Greasemonkey.

Editing the Script

At this point, Greasemonkey will make a copy of the script, into the scripts directory, and any future page loads (which match the include and exclude rules) will have that script run. At this point, though, "that script" is the copy that Greasemonkey made, not the original file you started with. It is probably a good idea now to delete the original file you created just moments ago, to reduce confusion.

Next, find and edit the copy of the script that Greasemonkey is working with. You may do this yourself, as explained in the FAQ, or by one of two shortcuts Greasemonkey provides. The first shortcut would be the edit button in the manage dialog. Alternately, you can use the right-click shortcut in the monkey menu.

Now that you are working with the copy of the script that Greasemonkey has installed, your changes will be immediately available (except for changes in metadata block, as mentioned here). When you make a change to the script, simply save the file and reload the page that it applies to, and this new version of the script will be run.

Picking an Editor

Most script wrights just use a familiar built in text editor such as Windows Notepad, Linux KDE Kwrite/Kate, Linux Gnome gedit, or Mac OS X TextEdit. However some may not include mono-spaced fonts, syntax highlighting and brace matching. These can be very helpful features when coding.

Common Text Editors
Title Java Linux Macintosh Windows Bracket Match Fixed Font Syntax Highlight
Aptana
Dreamweaver
Eclipse
EditPad Pro
gedit
KWrite/Kate
Notepad
Notepad2
Notepad++
Portable GVim
PSPad
TextEdit

That's It!

Writing scripts for Greasemonkey is not hard. What can get difficult, however, is working with the quirks and limitations that the script runs under; Some things that work perfectly in a normal web page, copied and pasted directly to a user script suddenly fail. It's important to know how the environment the scripts run in works.