Hack 99. Create Greasemonkey Scripts Automatically

 < Day Day Up > 

Meet Platypus, the graphical interface for Greasemonkey.

By this point, you have seen the awesome power of Greasemonkey to modify pages in myriad ways. But all these hacks have one thing in common: they require writing code. Sometimes that's unavoidable, but wouldn't it be nice to be able to make simple modifications without poking through the DOM or futzing with XPath?

Meet Platypus, the graphical interface for Greasemonkey.

12.6.1. Running the Hack

To install Platypus, go to http://platypus.mozdev.org and click the Install Platypus link. Firefox uses a whitelist to restrict sites from automatically installing browser extensions. If you see a message at the top of the window that says that Firefox has prevented this site from installing software, click Edit Options and add the site to your whitelist, as shown in Figure 12-8.

Figure 12-8. Firefox extensions whitelist


Now, click Install Platypus again, and Firefox will prompt you to confirm that you really want to install it. Click Install, and then quit Firefox and relaunch it to complete the installation.

OK, now you should see a new option in the Tools menu named "Platypus!" Go to any web page, such as http://www.oreilly.com, and then select "Platypus!" from the Tools menu. Since this is the first time you have run Platypus, it will open its help window to show you all the available options, as shown in Figure 12-9.

Close the help window, and you can start making modifications to the loaded page. As you move the cursor around the page, Platypus will highlight individual elements in red, as shown in Figure 12-10.

Figure 12-9. Platypus help window


Figure 12-10. Element highlighted by Platypus


What can you do with highlighted elements? All kinds of fun stuff. Press R to relax the element, removing any fixed size and positioning. Press C to center it, or B to modify it to black text on a white background. Press E to erase it (leaving its space intact), or X to delete it entirely (collapsing other elements around it).

You can even move elements around. Press Ctrl-X to cut the element, and then move to another position in the page and press Ctrl-V to paste it.

Is that still not enough control for you? Highlight any element and press S to bring up its style properties, as shown in Figure 12-11.

Figure 12-11. Edit Content Style dialog


In this dialog, you can change the element's color, dimensions, border, and font properties to anything you like.

Still not satisfied? Highlight an element and press V to view its HTML source in another window, or press M to modify its HTML markup. Or position the cursor anywhere and press H to insert arbitrary HTML into the page at that point.

If this is all too much work for you, you can load any page and press A to autorepair the page. This finds the biggest element on the page, removes everything else, widens the element to span the entire browser window, removes any background images, and sets the text to black on a white background. It doesn't work on every page, but it is especially useful on busy news article pages where you need to fight the distractions of the navigation bar, the sidebars, the towering ad banners, and the cluttered page footer to get to the actual content of the article.

So what does all this have to do with Greasemonkey? Once you've finished your modifications and you're happy with the page, press Ctrl-S to save your modifications as a Greasemonkey script. Platypus will even pop up the standard Greasemonkey install dialog and automatically install it for you, as shown in Figure 12-12.

Figure 12-12. Saving a Platypus mod as a Greasemonkey script


Once it's installed, you can open the Manage User Scripts dialog from the Tools menu, select the script, and click Edit to see how it works. Platypus exposes API functions for each of the modifications you made by pointing and clicking. The autogenerated Greasemonkey script calls Platypus API functions to replay the modifications every time you visit the page. This means that these Greasemonkey scripts will work only as long as you have Platypus installed.

     < Day Day Up > 


    Greasemonkey Hacks
    Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox
    ISBN: 0596101651
    EAN: 2147483647
    Year: 2005
    Pages: 168
    Authors: Mark Pilgrim

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net