Hack #2. Provide a Default Configuration
User scripts can be self-describing; they can contain information about what they do and where they should run by default.
Every user script has a section of metadata, which tells Greasemonkey about the script itself, where it came from, and when to run it. You can use this to provide users with information about your script, such as its name and a brief description of what the script does. You can also provide a default configuration for where the script should run: one page, one site, or a selection of multiple sites.
The Code
Save the following user script as helloworld.user.js:
Example: Hello World metadata
// ==UserScript==
// @name Hello World
// @namespace http://www.oreilly.com/catalog/greasemonkeyhcks/
// @description example script to alert "Hello world!" on every page
// @include *
// @exclude http://oreilly.com/*
// @exclude http://www.oreilly.com/*
// ==/UserScript==
alert('Hello world!');There are five separate pieces of metadata here, wrapped in a set of Greasemonkey-specific comments.
Wrapper
Let's take them in order, starting with the wrapper:
// ==UserScript== // // ==/UserScript==
These comments are significant and must match this pattern exactly. Greasemonkey uses them to signal the start and end of a user script's metadata section. This section can be defined anywhere in your script, but it's usually near the top.
Name
Within the metadata section, the first item is the name:
// @name Hello World
This is the name of your user ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access