Hack #4. Prevent a User Script from Executing

You can disable a user script temporarily, disable all user scripts, or uninstall a user script permanently.

Once you have a few user scripts running, you might want to temporarily disable some or all of them. There are several different ways to prevent a user script from running.

Disabling a User Script Without Uninstalling It

The easiest way to disable a user script is in the Manage User Scripts dialog. Assuming you installed the Butler user script [Hack #1] , you can disable it with just a few clicks:

  1. From the menu bar, select Tools Manage User Scripts…. Greasemonkey will pop up the Manage User Scripts dialog.

  2. In the left pane of the dialog is a list of all the user scripts you have installed. (If you've been following along from the beginning of the book, this will include just one script: Butler.)

  3. Select Butler in the list if it is not already selected, and deselect the Enabled checkbox. The color of Butler in the left pane should change subtly from black to gray. (This is difficult to see while it is still selected, but it's more useful once you have dozens of scripts installed.)

  4. Click OK to exit the Manage User Scripts dialog.

Now, Butler is installed, but inactive. You can verify this by searching for something on Google. It should no longer say "Enhanced by Butler" along the top. You can reenable the Butler user script by repeating the procedure and reselecting the Enabled checkbox in the Manage User Scripts dialog.

Tip

Once disabled, a user script will remain disabled until you manually reenable it, even if you quit and relaunch Firefox.

Disabling All User Scripts

While Greasemonkey is installed, it displays a little smiling monkey icon in the status bar, as shown in Figure 1-5.

Greasemonkey status bar icon

Figure 1-5. Greasemonkey status bar icon

Clicking the Greasemonkey icon in the status bar disables Greasemonkey entirely; any user scripts you have installed will no longer execute. The Greasemonkey icon will frown and turn gray to indicate that Greasemonkey is currently disabled, as shown in Figure 1-6.

Clicking the icon again reenables Greasemonkey and any enabled user scripts.

Greasemonkey disabled

Figure 1-6. Greasemonkey disabled

Disabling a User Script by Removing All Included Pages

As shown in "Master the @include and @exclude Directives" [Hack #3] , user scripts contain two sections: a list of pages to run the script and a list of pages not to run the script. Another way to prevent a user script from executing is to remove all the pages on which it runs:

  1. From the menu bar, select Tools Manage User Scripts…. Greasemonkey will pop up the Manage User Scripts dialog.

  2. In the left pane of the dialog is a list of all the user scripts you have installed.

  3. Select Butler in the list if it is not already selected, and then select http://*.google.com/* in the list of Included Pages. Click the Remove button to remove this URL from the list.

  4. Click OK to exit the Manage User Scripts dialog.

Disabling a User Script by Excluding All Pages

Yet another way to disable a user script is to add a wildcard to exclude it from all pages:

  1. From the menu, select Tools Manage User Scripts…. Greasemonkey will pop up the Manage User Scripts dialog.

  2. In the left pane of the dialog is a list of all the user scripts you have installed.

  3. Select Butler in the list if it is not already selected.

  4. Under the Excluded Pages list, click the Add button. Greasemonkey will pop up an Add Page dialog box. Type * and click OK.

  5. Click OK to exit the Manage User Scripts dialog.

Now, Butler is still installed and technically still active. But because excluded pages take precedence over included pages, Butler will never actually be executed, because you have told Greasemonkey to exclude it from all pages.

Disabling a User Script by Editing config.xml

As shown in "Install a User Script" [Hack #1] , Greasemonkey stores the list of installed scripts in a configuration file, config.xml, deep within your Firefox profile directory:

	<UserScriptConfig>
		<Script filename="butler.user.js"
				name="Butler"
				namespace="http://diveintomark.org/projects/butler/"
				description="Link to competitors from Google search results"
				enabled="true">
			<Include>http://*.google.com/*</Include>
		</Script>
	</UserScriptConfig>

You can manually edit this file to disable a user script. To disable Butler, find its <Script> element in config.xml, and then set the enabled attribute to false.

Uninstalling a User Script

Finally, you can remove a user script entirely by uninstalling it:

  1. From the menu bar, select Tools Manage User Scripts…. Greasemonkey will pop up a Manage User Scripts dialog.

  2. In the left pane, select Butler.

  3. Click Uninstall.

  4. Click OK to exit the Manage User Scripts dialog.

Butler is now uninstalled completely.

Get Greasemonkey Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.