[Greasemonkey] Basic JavaScript questions
mmccarthy at reifysoft.com
mmccarthy at reifysoft.com
Wed Jun 29 14:06:54 EDT 2005
You could do it with CSS, yeah. You just need a rule that says: "h3 {
display: none; }"
See "Adding CSS styles":
http://diveintogreasemonkey.org/patterns/add-css.html
Your original example should work, I think. Maybe something else is going
wrong. Your collection of H3s shuldn't be getting shorter because you're
removing the H3s from the DOM, not from the collection that
getElementsByTagName returns.
var h3s = document.getElementsByTagName("h3");
for (var i = 0; i < h3s.length; i++)
h3s[i].parentNode.removeChild(h3s[i]);
If you were removing items from the collection, it would look like this:
var h3s = ["foo", "bar", "baz", "blah"];
while (h3s.length > 0)
h3s.splice(0, 1);
.Which won't do the trick for you because it doesn't remove them from the
DOM.
Note that getElementsByTagName doesn't return an array. It just returns an
object with a length property and a hashtable (like any JS object) of DOM
nodes keyed by numbers from 0 up. So, array methods won't work on it.
To get the text node in an H3, assuming there is only text between the start
and end tags and no whitespace or other elements, myH3.firstChild will do.
To get the text string, myH3.firstChild.nodeValue will work. Note that
Mozilla might find white space as the first child node (IE ignores white
space), in which case it'll return that, though that doesn't look like it'll
be a problem on the site you pointed out.
Matt McCarthy
When I am king, you will be first against the wall
With your opinion, which is of no consequence at all.
-Radiohead, "Paranoid Android"
_____
From: greasemonkey-bounces at mozdev.org
[mailto:greasemonkey-bounces at mozdev.org] On Behalf Of The Chris Method
Sent: Wednesday, June 29, 2005 12:32 PM
To: greasemonkey at mozdev.org
Subject: [Greasemonkey] Basic JavaScript questions
I'm trying to write a basic script, one that removes all <h3> tags from a
certain webpage (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/fddad974/attachment.htm
More information about the Greasemonkey
mailing list