[Greasemonkey] Grab text string in TD

Aaron Boodman zboogs at gmail.com
Fri Apr 22 12:14:24 EDT 2005


*But*, you're often not looking for the best way, but the way that
works with minimal effort.

This is that way:

document.body.innerHTML = document.body.innerHTML.replace(/Google/ig, "monkey");

If this starts catching javascript or something that is required to
make the page work, then you can make it slicker with xpath. But why
bother if this works?

On 4/22/05, Mark Pilgrim <pilgrim at gmail.com> wrote:
> On 4/22/05, Edward Lee <edilee at gmail.com> wrote:
> > On 4/22/05, Paul Roub <paul at roub.net> wrote:
> > > Anyone have a simpler variation of this?
> >
> > Assuming you have something like this overlay function..
> > http://www.mozdev.org/pipermail/greasemonkey/2005-April/001173.html
> >
> > overlay('//text()', function(text) { text.nodeValue =
> > text.nodeValue.replace(/Google/g, 'Gooogle!'); });
> >
> > Note that it'll only replace next nodes and not something like the
> > value of the search button, so Google Search remains as that.
> 
> That overlay function is very nice.  I did something similar (without
> encapsulation) in DumbQuotes:
> 
> http://diveintomark.org/projects/greasemonkey/dumbquotes.user.js
> 
> But your XPath query is simpler and probably ends up being equivalent.
> 
> Bottom line: the best way to do a text search/replace is to loop
> through all the text nodes and do the replacement in each of them.
> And the best way to loop through all the text nodes is with an XPath
> query of "//text()".
> 
> --
> Cheers,
> -Mark
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>


More information about the Greasemonkey mailing list