Hack 80. Break Out of Frames

 < Day Day Up > 

Replace a framed document with the biggest single frame.

On some news sites, link collections or weblogs, and even web mail programs, external links are wrapped into a frame. This wastes your precious screen real estate to keep part of the original web site in view, usually with additional advertisements. We can take back control of our browsing experience and use the whole screen instead of wasting it on these framed wrappers.

9.5.1. The Code

This script takes a straightforward approach, which comes in three simple steps. First, via XPath, we find all the <frame> elements. Then, we check the site and location of each frame and save it as if it is the biggest frame so far.

Once the loop has evaluated all <frame> tags, one of two things happens. If we have a URL of the biggest frame recorded, the script redirects to that URL with the location.replace method. If no URL has been recorded because there are no frames on this page, we do nothing. This makes the script safe to include with wide-open wildcards that match an entire domain.

The default list of included pages is simply an example page. There are many legitimately frames-based pages, so a catchall @include * parameter would disrupt too many sites. You will need to manually add pages that you know have wrapped frames.

Save the following user script as frame-killer-plus.user.js:

 // ==UserScript== // @name   Frame Killer Plus // @namespace   http://www.arantius.com/ // @description   Replaces the current page with the biggest frame // @include       http://www.example.com/ // ==/UserScript== // based on code by Anthony Lieuallen // and included here with his gracious permission var i=0,f,bigArea=-1,frameArea,newLoc=''; // use xpath here to circumvent security restrictions that prevent // reading the src directly var frames=document.evaluate("//frame", document, null,     XPathResult.ANY_TYPE, null); while (f = frames.iterateNext()) {     frameArea = (parseInt(f.offsetWidth) *             parseInt(f.offsetHeight)); if (frameArea > bigArea) { bigArea = frameArea; newLoc = f.src; } } if (''!=newLoc) { document.location.replace(newLoc); } 

9.5.2. Running the Hack

The web site About.com (http://www.about.com) places a large frame in the top of your window when you click through to an external page. The top frame includes some advertisements and a link back to About.com; in other words, it's useless.

Install the user script (Tools Install This User Script), but dont change the @include parameter. (We'll change it in a minute.) Now, go to http://atheism.about.com/od/offsiteagnostic/, shown in Figure 9-7.

Figure 9-7. Offsite links on About.com


Click the link titled Agnostic Church Home Page, and you will see the external site wrapped in an About.com frame, as shown in Figure 9-8.

Now, go to Tools Manage User Scripts, select Frame Killer Plus in the pane on the left, click Add… next to the list of included pages, and enter http://*.about.com/*offsite*. Click OK to exit the Manage User Scripts dialog, go back to http://atheism.about.com/od/offsiteagnostic/, and again click the link titled Agnostic Church Home Page. This time, the script will kill the useless About.com frame wrapper and redirect you straight to the external site.

Anthony Lieuallen

Figure 9-8. External site wrapped in About.com frame


     < 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