Menu

Greasemonkey_Manual:Creating_Scripts

mmartz

Getting StartedInstalling ScriptsManaging ScriptsMonkey Menu ⬌ Creating Scripts ⬌ EnvironmentOther Useful ToolsAPI

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 it 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 or OpenUserJS.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 | Editing the Script | Changing the Editor | That’s It!

Picking an Editor

Most script wrights use a familiar built in text editor such as Windows Notepad, Linux KDE Kwrite/Kate, Linux Gnome gedit, Mac OS X TextEdit or in newer releases of Firefox the Scratchpad editor. However some may not include mono-spaced fonts, syntax highlighting and brace matching. These can be very helpful features when coding. For more information, and choices, see Wikipedia’s comparison of text editors.

Creating the Script

Begin with creating the text file in a favorite plain text editor. It should contain the Metadata Block with at least the @name, @namespace, a brief @description and any appropriate @include and @exclude rules. Once this has been completed continuation of writing the actual code may occur. However it is not yet necessary, and can be considered counterproductive for new script wrights. Now the script stub is ready to be installed into Greasemonkey.

Editing the Script

At this point Greasemonkey has already made a copy of the script installed and placed it in the Scripts directory. Any future page loads which match the Include and exclude rules will execute the script. Close the text editor and optionally delete the original source file.

Next is finding and editing the copy of the script that Greasemonkey is working with. This may be done directly as explained in the FAQ, or by one of two shortcuts Greasemonkey provides. The first shortcut would be the edit button in the management interface. Alternately a quick right-click in the monkey menu will produce the same results.

Working with the copy of the script that Greasemonkey has installed, code changes will be immediately available excluding certain changes in the metadata block mentioned here. When all changes are completed, simply save the file and reload any pages that apply.

Changing the Editor

Depending on which version of Firefox is installed there is now an option in the preferences interface that allows changing of the default editor. The easiest way of changing the default editor is to open the Add-ons Manager or Add-ons dialog, find Greasemonkey in the Extension list and click on the preferences button:

Add-ons Manager Firefox 4.x+Preferences Interface

NOTE: Changing the default editor manually is still available via the about:config page. Find the about:config?filter=extensions.greasemonkey.editor preference. Double-click it, and type or paste in the location of the script editor to use.

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.

Getting StartedInstalling ScriptsManaging ScriptsMonkey Menu ⬌ Creating Scripts ⬌ EnvironmentOther Useful ToolsAPI

Notes


Related

Wiki: Greasemonkey_Manual:API
Wiki: Greasemonkey_Manual:Environment
Wiki: Greasemonkey_Manual:Getting_Started
Wiki: Greasemonkey_Manual:Installing_Scripts
Wiki: Greasemonkey_Manual:Managing_Scripts
Wiki: Greasemonkey_Manual:Monkey_Menu
Wiki: Greasemonkey_Manual:Other_Useful_Tools
Wiki: Metadata_Block
Wiki: Script_injection_rules
Wiki: Scripts_directory
Wiki: User_script

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.