[Greasemonkey] Basic JavaScript questions

chris feldmann cfeldmann at gmail.com
Wed Jun 29 17:42:25 EDT 2005


I was wrong. it was removing every *other* h3. it would get halfway down, 
the iterator would get halfway up, and they'd bump into each other in the 
middle. This works, though:

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

all = document.evaluate(
"//h3[@class='title']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);

//all = document.getElementsByTagName("h3");
var z = all.length;
for (i = 0; i < all.snapshotLength; i++) {
e = all.snapshotItem(i);
GM_log('removing node ' + e.firstChild.nodeValue + ' all.length: ' + 
all.length+' i: ' +i+'z: '+z);
e.parentNode.removeChild(e);
}
})();

 Something about using document.getElementsByTagName() makes it unuseful for 
this. XPath on the other hand provides an array that can be cleanly iterated 
through.




On 6/29/05, The Chris Method <thechrisproject at gmail.com> wrote:
> 
> Here's the whole shebang so far:
> 
> // ==UserScript==
> // @name Overheard in NY cleaner
> // @namespace http://thechrisproject.com
> // @description example script to alert "Hello world!" on every page
> // @include http://overheardinnewyork.com/*
> // @include http://www.overheardinnewyork.com/*
> // ==/UserScript==
> 
> (function() {
> var e, i, all;
> 
> all = document.getElementsByTagName("h3");
> for (i = 0; all.length > 0; i++) {
> e = all[i];
> GM_log('i:' + i + ' removing node:' + e.firstChild.nodeValue + ' 
> all.length:' + all.length);
> e.parentNode.removeChild(e);
> }
> })();
> 
> On today's overheardinnewyork page, it logs 18 iterations of the loop. The 
> all.length value is one less every iteration, as chris indicated above. 
> The next statement in the console is an error that e has no properties. When 
> looking at the page, there are still quite a few h3 tags showing.
> 
> On 6/29/05, chris feldmann <cfeldmann at gmail.com> wrote:
> > 
> > Sorry, accidentally sent. Anyway, I wanted to note that simply cut and 
> > pasted, your script works just fine for me. The log looks like this:
> > 
> > /inner: removing node [object Text] all.length: 21
> > /inner: removing node [object Text] all.length: 20
> > /inner: removing node [object Text] all.length: 19
> > /inner: removing node [object Text] all.length: 18
> > 
> > 
> > And all the H3's are gone. Possibly the include parameter is not being 
> > set correctly on install (I seem to have to do it manually as a rule)?
> > 
> > On 6/29/05, chris feldmann <cfeldmann at gmail.com > wrote:
> > > 
> > > 1. e.nodeValue
> > > 2. is it maybe getting shorter because you're removing a node on each 
> > > iteration?
> > > 
> > > And when you say it's doing nothing, you mean other than logging, 
> > > right? What exactly is getting logged?
> > > 
> > > 
> > > On 6/29/05, The Chris Method < thechrisproject at gmail.com> wrote:
> > > 
> > > > 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<http://en.wikipedia.org/wiki/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? 
> > > > _______________________________________________
> > > > Greasemonkey mailing list
> > > > Greasemonkey at mozdev.org 
> > > > http://mozdev.org/mailman/listinfo/greasemonkey
> > > > 
> > > > 
> > > > 
> > > 
> > 
> > _______________________________________________
> > Greasemonkey mailing list
> > Greasemonkey at mozdev.org 
> > http://mozdev.org/mailman/listinfo/greasemonkey
> > 
> > 
> > 
> 
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
> 
> 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mozdev.org/pipermail/greasemonkey/attachments/20050629/8c23f6f5/attachment-0001.htm


More information about the Greasemonkey mailing list