| < Day Day Up > |
Got a Hack?To explore Hacks books online or to contribute a hack for future titles, visit:
|
| < Day Day Up > |
| < Day Day Up > |
Chapter 1. Getting Started
Section 1.1. Hacks 1-12: Introduction Hack 1. Install a User Script Hack 2. Provide a Default Configuration Hack 3. Master the @include and @exclude Directives Hack 4. Prevent a User Script from Executing Hack 5. Configure a User Script Hack 6. Add or Remove Content on a Page Hack 7. Alter a Page's Style Hack 8. Master XPath Expressions Hack 9. Develop a User Script "Live" Hack 10. Debug a User Script Hack 11. Embed Graphics in a User Script Hack 12. Avoid Common Pitfalls |
| < Day Day Up > |
| < Day Day Up > |
1.1. Hacks 1-12: IntroductionThe first thing you need to do to get started with Greasemonkey is install it. Open Firefox and go to http://greasemonkey.mozdev.org. Click the Install Greasemonkey link. Firefox will warn you that it prevented this site from installing software, as shown in Figure 1-1. Figure 1-1. Firefox, requiring you to whitelist sites to install extensionsClick the Edit Options button to bring up the Allowed Sites dialog, as shown in Figure 1-2. Figure 1-2. Allowed Sites dialog
Click the Allow button to add the Greasemonkey site to your list of allowed sites; then click OK to dismiss the dialog. Now, click the Install Greasemonkey link again, and Firefox will pop up the Software Installation dialog, as shown in Figure 1-3. Figure 1-3. Software Installation dialog
Click Install Now to begin the installation process. After it downloads, quit Firefox and relaunch it to finish installing Greasemonkey. Now that that's out of the way, let's get right to it. |
| < Day Day Up > |
| < Day Day Up > |
Hack 1. Install a
|
|
This hack shows three ways to install user scripts. The first user script I ever wrote was called
Here's how to install Butler from the context menu:
Visit the Butler home page (http://diveintomark.org/projects/butler/) to see a brief description of the functionality that Butler offers.
Right-click (Control-click on a Mac) the link titled "Download version…" (at the time of this writing, Version 0.3 is the latest release).
From the context menu, select Install User Script….
A dialog titled Install User Script will pop up, displaying the
Click OK to install the user script.
If all went well, Greasemonkey will display the following alert: "Success! Refresh page to see changes."
Now, search for something in Google. In the search results page, there is a line at the top of the results that says "Try your search on: Yahoo, Ask Jeeves, AlltheWeb…" as shown in Figure 1-4. There is also a banner along the top that says "Enhanced by Butler." All of these options were added by the Butler user script.
My Butler user script has a home page, but not all scripts do. Sometimes the author posts only the script itself. You can still install such scripts, even if there are no links to right-click.
Visit http://diveintomark.org/projects/butler/butler.user.js. You will see the Butler source code displayed in your browser. From the Tools menu, select Install User Script…. Greasemonkey will pop up the Install User Script dialog, and the rest of the installation is the same as described in the previous section.
Like most Firefox browser extensions, Greasemonkey stores its configuration files in your Firefox profile directory. You can install a user script manually by placing it in the right directory and editing the Greasemonkey configuration file with a text editor.
First you'll need to find your Firefox profile directory, which is harder than it sounds. The following list, from Nigel MacFarlane's
C:\Windows\Application Data\Mozilla\Firefox
C:\Windows\Profiles\%USERNAME%\Application Data\Mozilla\Firefox
C:\Winnt\Profiles\%USERNAME%\Application Data\Mozilla\Firefox
C:\Documents and Settings\%USERNAME%\Application Data\Mozilla\Firefox
~/.mozilla/firefox
~/Library/Application Support/Firefox
Within your Firefox directory is your
Profiles
directory, and within that is a
<UserScriptConfig> <Script filename="bloglinesautoloader.user.js" name="Bloglines Autoloader" namespace="http://diveintomark.org/projects/greasemonkey/" description="Auto-display all new items in Bloglines (the equivalent of clicking the root level of your subscriptions)" enabled="true"> <Include>http://bloglines.com/myblogs*</Include> <Include>http://www.bloglines.com/myblogs*</Include> </Script> <Script filename="googlesearchkeys.user.js" name="Google Searchkeys" namespace="http://www.imperialviolet.org" description="Adds one-press access keys to Google search results" enabled="true"> <Include>http://www.google.*/search*</Include> </Script> <Script filename="mailtocomposeingmail.user.js" name="Mailto Compose In GMail" namespace="http://blog.monstuff.com/archives/000238.html" description="Rewrites "mailto:" links to GMail compose links" enabled="true"> <Include>*</Include> <Exclude>http://gmail.google.com</Exclude> </Script> </UserScriptConfig>
To install a new script, simply copy it to this scripts directory and add a <Script> entry like the other ones in config.xml . The <Script> element has five attributes: filename, name, namespace, description , and enabled . Within the <Script> element you can have multiple <Include> and <Exclude> elements, as defined in "Provide a Default Configuration" [Hack #2] .
For example, to manually install the Butler user script, copy the butler.user.js file into your scripts directory, and then add this XML snippet to config.xml , just before </UserScriptConfig> :
<Script filename="butler.user.js" name="Butler" namespace="http://diveintomark.org/projects/butler/" description="Link to competitors in Google search results" enabled="true"> <Include>*</Include> <Exclude>http://*.google.*/*</Exclude> </Script>
|
| < Day Day Up > |