Noob question - converting selected links to plain text.

in Script development
Subscribe to Noob question - converting selected links to plain text. 4 posts, 3 voices



Charlie711 User
Firefox

I'm having trouble changing links in a page.

For example on this page - http://forum.weatherzone.com.au/ubbthreads.php/...

I would like to convert the link "radar" in the user's post to plain text.

(function(){
var radarlink = document.body.innerHTML.match("<a href='http://www.weatherzone.com.au/radar/'> radar</a> ");

if (radarlink != null)
{
if (radarlink.length > 0)
{
document.body.innerHTML = document.body.innerHTML.replace("<a href='http://www.weatherzone.com.au/radar/'> radar</a> ",'Radar');

}
}

})();

I've tried the above simple replace, but I've clearly missed something very basic and presumably quite important.

Can anyone please give me a clue as to where I'm going wrong?

 
Kastro187420 Scriptwright
FirefoxWindows

Well, I'm not entirely sure, but it seems like there are certain things in the script that shouldn't be there.

(function(){
var radarlink = document.body.innerHTML.match("<a href='http://www.weatherzone.com.au/radar/'> radar</a> ");

if (radarlink != null)
{
if (radarlink.length > 0)
{
document.body.innerHTML = document.body.innerHTML.replace("<a href='http://www.weatherzone.com.au/radar/'> radar</a> ",'Radar');

}
}

})();

For instance, just before the word "function", there's an open (, whose purpose seems beyond me at this point, aswell one at the end, along with an additional (); that seems like it serves no purpose either. Unless there is more to your script than what I'm seeing here, that might be part of the issue.

 
Charlie711 User
Firefox

Thanks for your reply.

I found the problem. It seems like replace() was treating the strings as mangled regexes for some reason. I escaped the slashes and it's working properly now.

 
AmpliDude Scriptwright
FirefoxWindows

Modyfing innerHTML of body isn't a very good idea. Since you know exactly the url of a link why not replace it:

link = document.querySelector('a[href="http://www.weatherzone.com.au/radar/"]');
if (link != null)
    link.parentNode.replaceChild(document.createTextNode("radar"), link);