tags:

views:

49

answers:

2

I'm writing a simple Greasemonkey script to strip out all the images, headings and paragraphs from a web page (it's because someone wants to use images of several popular websites with the images and text removed in a presentation about branding of websites). I've figured out how to do so with images by using a for loop and the document.images array like this:

var i = 0;
var imglngth = document.images.length;

for(i=0;i<imglngth;i++)
{
    document.images[i].style.display="none";
}

However, I don't believe there's an object representing paragraphs or headers. Any suggestions as to how I could implement this?

+1  A: 

Using jQuery would be very each to get all the <p> elements.

$('p').each(function(){
   // do stuff
});

Here's a link for some info on jQuery and GreaseMonkey

gmcalab
Will jQuery work with Greasemonkey?
mattbd
As long as you can include the jquery script, it may. It's worth a shot?
gmcalab
Thanks, that worked OK. It's very rough and ready as I haven't really used jQuery, and I can't get it to work in Chrome, but it works perfectly in Firefox and as long as it works in one browser it should be fine.
mattbd
Whomever down-voted, I'd like to know why.
gmcalab
+1  A: 

If the headers are in <h1> - <h6> tags, you can use the getElementsByTagName method from the DOM API, and a loop to get everything from <h1> to <h6>.

for( i = 1; i<= 6; i++ ) { 
    var tag = 'h' + i;
    var heads = document.getElementsByTagName( tag );
    for ( j = 0; j < heads.length; j++ ) { 
        heads[j].style.display = 'none';
    }
}

If the document does not use header tags, you may have to reverse-engineer their stylesheet and do something with a bit more logic.

Of course, manually traversing the DOM API can be a pain. If you can use something like jQuery, it's a bit easier:

$( ':header' ).each( function() { ... } );
friedo
FYI Greasemonkey scripts can use `Array.forEach` to iterate a NodeList. Also, jQuery has the `":header"` psuedo selector for your hx tag selector there.
Roatin Marth
The jQuery selector should be `h1, h2, h3, h4, h5, h6` (note the commas)
Felix
Ooh, didn't know about `:header`. I'll update it, thanks
friedo
I would not generally use `:header` unless I had to (eg. to do `:header+:header`, which would be an unpleasant rule to express without it). As this is a non-standard selector, it forces jQuery to use the slow JavaScript Sizzle library instead of the fast `querySelectorAll` method built into modern browsers.
bobince