[Greasemonkey] Basic JavaScript questions

The Chris Method thechrisproject at gmail.com
Wed Jun 29 15:32:29 EDT 2005


I'm trying to write a basic script, one that removes all <h3> tags from a 
certain webpage (overheardinnewyork.com <http://overheardinnewyork.com>). I 
have some pretty basic JavaScript questions:

1) In debugging my code, I've been trying to figure out a way to access the 
info inside the tags, i.e. the "text" in <h3>text</h3>. If 'e' is a node 
representing this element, how do I get the text inside? e.firstChild gives 
me [object Text] and e.firstChild.value gives me 'undefined'.

2) My first guess on how to actually accomplish the aforementioned task was 
this:

(function() {
var e, i, all;

all = document.getElementsByTagName("h3");
for (i = 0; i < all.length; i += 1) {
e = all[i];
GM_log('removing node ' + e.firstChild + ' all.length: ' + all.length);
e.parentNode.removeChild(e);
}
})();


This does nothing. 'all', which is supposed to be a read-only array 
(according to my JS ref book), gets one item shorter every time I iterate 
through the for loop, and i increments. So then I change the condition on 
the loop from i<all.length to all.length>0. This removes them all. But it 
seems ugly to me. I guess I'm looking for code critiques and ideas for 
better ways to do this. Could this all be done with some sort of css fun? 

If my approach is to just remove the h3 elements entirely, what is the best 
way?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mozdev.org/pipermail/greasemonkey/attachments/20050629/f16a22b6/attachment.htm


More information about the Greasemonkey mailing list