Sams Teach Yourself JavaScript in 24 Hours Free Open Book

Sams Teach Yourself JavaScript in 24 Hours

Previous Page
Next Page

Introducing Greasemonkey

So far in this book, you've been using JavaScript to work on your own sites. In this hour, you'll take a break from that and learn about a way to use JavaScript on other people's sites. Greasemonkey is an extension for the Firefox browser that enables user scripts. These are scripts that run as soon as you load a page and can make changes to the page's DOM.

A user script can be designed to work on all web pages, or only to affect particular sites. Here are some of the things user scripts can do:

  • Change the appearance of one or more sitescolors, font size, and so on.

  • Change the behavior of one or more sites with JavaScript.

  • Fix a bug in a site before the site author does.

  • Add a feature to your browser, such as text macrossee the Try It Yourself section of this hour for an example.

As a simple example, a user script called Linkify is provided with Greasemonkey. It affects all pages you visit and turns unlinked URLs into hyperlinks. In other words, the script looks for any string that resembles a URL in the page, and if it finds a URL that is not enclosed in an <a> tag, it modifies the DOM to add a link to the URL.

Greasemonkey scripts can range from simple ones such as Linkify to complex scripts that add a feature to the browser, rearrange a site to make it more usable, or eliminate annoying features of sites such as pop-up ads.

Keep in mind that Greasemonkey doesn't do anything to the websites you visitit strictly affects your personal experience with the sites. In this way, it's similar to other browser customizations, such as personal style sheets and browser font settings.

By the Way

Greasemonkey was created in 2004 by Aaron Boodman. Its official site is http://greasemonkey.mozdev.org/. At this writing, the current version of Greasemonkey is 0.6.4. The current developers are Aaron Boodman and Jeremy Dunck.


Installing Greasemonkey in Firefox

Greasemonkey works in Firefox for Windows, Macintosh, and Linux platforms. You can install it by visiting the Greasemonkey site and running the installer. Start at http://greasemonkey.mozdev.org/ and follow these steps:

1.
Click the Install Greasemonkey link.

2.
You will probably see a message in a yellow bar at the top of the window warning you about installing software. Click the Edit Options button within the yellow bar.

3.
In the Allowed Sites dialog, shown in Figure 18.1, click the Allow button to allow the current site to install software, and then click Close.

Figure 18.1. Firefox prompts you to allow a site for installing extensions.


4.
Click the Install link again, and then click the Install button in the Software Installation dialog that appears.

5.
Exit and restart Firefox. You should see a small monkey icon in the lower-right hand corner of the browser window if the extension was successfully installed.

By the Way

When you first install Greasemonkey, the extension doesn't do anythingyou'll need to install one or more user scripts, as described in the next section, to make it useful.


Turnabout for Internet Explorer

Greasemonkey was written as a Firefox extension, and does not work on other browsers. Fortunately, there's an alternative for those who prefer Internet Explorer: Turnabout, from Reify, is an open-source add-on for Internet Explorer that supports user scripts. Turnabout is available for free from its official site at http://www.reifysoft.com/turnabout.php. Two versions are available:

  • Turnabout Basic, which only supports the scripts bundled with it

  • Turnabout Advanced, which supports any user script, similar to Greasemonkey

Turnabout supports most of Greasemonkey's features, and user scripts for Greasemonkey often work with Turnabout Advanced without modification. The only potential problem is with differences in JavaScript and in the DOM between Internet Explorer and Firefox. If you follow the same cross-browser coding practices you learned throughout this book, there's a good chance you can make a user script that works on both platforms.

Other Browsers

Although Greasemonkey itself is still relatively new software, user script features have also appeared for other browsers. Along with Turnabout for IE, two other browsers can support user scripts:

  • Opera, the cross-platform browser from Opera Software ASA, has built-in support for user scripts, and supports Greasemonkey scripts in many cases. See Opera's site for details at http://www.opera.com/.

  • Creammonkey is a beta add-on for Apple's Safari browser to support user scripts. You can find it at http://8-p.info/Creammonkey/.

User Script Security

Before you get into user scripting, a word of warning: Don't install a script unless you understand what it's doing, or you've obtained it from a trustworthy source. Although the Greasemonkey developers have spent a great deal of time eliminating security holes, it's still possible for a malicious script to cause you troubleat the very least, it could send information about which sites you visit to a third-party website.

To minimize security risks, be sure you're running the latest version of Greasemonkey or Turnabout. Only enable scripts you are actively using, and limit scripts you don't trust to specific pages so they don't run on every page you visit.

Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


     Main Menu
Sams Teach Yourself JavaScript in 24 Hours
Table of Contents
Copyright
About the Author
Acknowledgments
Part I: Introducing the Concept of Web scripting and the JavaScript Language
Part II: Learning JavaScript Basics
Part III: Learning More About the DOM
Part IV: Working with Advanced JavaScript Features
Hour 15. Unobtrusive Scripting
Hour 16. Debugging JavaScript Applications
Hour 17. AJAX: Remote Scripting
Hour 18. Greasemonkey: Enhancing the Web with JavaScript
Introducing Greasemonkey
Working with User Scripts
Creating Your Own User Scripts
Summary
Q&A
Quiz Questions
Quiz Answers
Exercises
Part V: Building Multimedia Applications with JavaScript
Part VI: Creating Complex Scripts
Part VII: Appendixes
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele