Dive Into Greasemonkey

Teaching an old web new tricks

2.1. Hello World

Our hundred-mile-long journey into the wonderful world of Greasemonkey scripting begins with a single step, and it is a step familiar to all readers of technical manuals: getting your computer to print out “Hello world”.

Example: helloworld.user.js

// Hello World! example user script
// version 0.1 BETA!
// 2005-04-22
// Copyright (c) 2005, Mark Pilgrim
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Hello World", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Hello World
// @namespace     http://diveintogreasemonkey.org/download/
// @description   example script to alert "Hello world!" on every page
// @include       *
// @exclude       http://diveintogreasemonkey.org/*
// @exclude       http://www.diveintogreasemonkey.org/*
// ==/UserScript==

alert('Hello world!');

As you can see, most of the lines in the Hello World user script are comments. Some of these comments, like the instructions on how to install it, have no special meaning; they are simply there for the benefit of clueless end users. However, there is a section in the comments that does have special meaning, as you'll see in the next section.

To see the user script in action, you should install it in the usual way, then visit a page outside the diveintogreasemonkey.org domain (for example, Google). The page should display as normal, but an alert will pop up saying “Hello world!

← Your First User Script
Describing your user script with metadata →