[Greasemonkey] newsbie-question how to change and replace a link with greasemonkey

Charles Iliya Krempeaux supercanadian at gmail.com
Sun Feb 5 15:28:15 EST 2006


Hello,

On 2/5/06, Jeremy Dunck <jdunck at gmail.com> wrote:
>
> On 2/5/06, Charles Iliya Krempeaux <supercanadian at gmail.com> wrote:
> > Hello Tina,
> >
> > On 2/5/06, Tina Weller <blond.woman at googlemail.com> wrote:
> > > is it possible to do the following with Greasemonkey?
> ...
> > Yes, it is possible.  (And, depending on your JavaScript experience)
> pretty
> > easy to do.
>
> And now, Charles, please dazzle us with your JavaScript experience.


Sure....

Note first, I wasn't completely sure what form Tina has her links coming
in.  But, I'm my code takes a link like this:

    http://www.xyz.de%2Fdata%2Ffile%2Ehtm

and change it like this:

    http://www.aaa.de/data/file.htm

Although I'm not really sure why the "href" of an <a> tag would have it in
that form.  (But maybe that's her whole problem!)  But anyways... that's
what my code reflects.

Note, this is NOT tested at all, but hopefully you get the idea.


var needle = "http://www.xyz.de";

var a_elements = document.getElementsByTagName("a");

if (  a_elements && a_elements.length  ) {
    for (var i=a.elements.length-1; i>-1; i--) {
        var a_element = a_elements[i];
        var a_href = a_element.getAttribute("href");
        if ( a_href ) {
            if (  (a_href.length >= needle.length) && (needle ==
a_href.substring(0,needle.length))  ) {
                var new_href = unescape(a_href);
                new_href = "http://www.aaa.de" + a_href.substring(
needle.length);

                a_element.setAttibute("href",new_href);
            }
        }
    } // for
}



See ya

--
    Charles Iliya Krempeaux, B.Sc.

    charles @ reptile.ca
    supercanadian @ gmail.com

    developer weblog: http://ChangeLog.ca/
___________________________________________________________________________
Make Television                                http://maketelevision.com/


More information about the Greasemonkey mailing list