Add jQuery to your Greasemonkey script

Greasemonkey is a Firefox add-on that lets you run JavaScript on web pages you specify. Using Greasemonkey you can improve and change the way websites look and behave (see examples of what is possible here). To learn more about it see it's official site, to learn how to include and write your own custom scripts (aka "user scripts") visit coder's manual.

Usually Greasemonkey scripts work with the DOM and jQuery is the best way to do that. So, no wonder you'll want to include jQuery into your custom scripts. Here are 3 ways to include jQuery library into your user script.

If you are targeting for Greasemonkey 0.8.0+ you can use @require. It runs once when the script is being installed, hence downloaded once. This is preferred and most efficient way to add jQuery.

// ==UserScript==
// @name     My jQuery Test
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==

// Do your magic here
$(".ads").remove();
If you are developing for older versions of Greasemonkey you have 2 options. Either add the whole jQuery library code into your Greasemonkey script file or dynamically add a <script> tag to the page.

Adding jQuery library right in your custom script

// ==UserScript==
// @name     My jQuery Test
// ==/UserScript==

// include jQuery library
/*!
 * jQuery JavaScript Library v1.6.2
 * http://jquery.com/
 *
 * Copyright 2011, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 * Copyright 2011, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: Thu Jun 30 14:16:56 2011 -0400
 */
(function(a,b){function cv(a){return f.isWindow(a)? ...

One more method to add jQuery

One more way to add jQuery is by attaching <script> tag to the document. You can read more about it and grab the code here.

Don't forget to check if jQuery is already loaded

if (unsafeWindow.jQuery === undefined) {
  // jQuery is NOT loaded
} else {
  // jQuery is already loaded
}