3

In some forum I join, they just replace some link with something like spam or deleted. Example: www.rapidshare.com/download/123 will automatically turn to www.spam.com/download/123 OR word MONEY will change to BOSS.

Its really annoyed me because I have to rename back manually if I want to download. Is there any Javascript that can solve this that will replace back www.spam.com to www.rapidshare.com? I mean in client side.

Thanks

2 Answers 2

3

If these URLs are in href attributes...

var replaceHrefAttributes = function (element, search, replace) {
    var nodes = element.getElementsByTagName('a');

    for (var i = 0, length = nodes.length; i < length; i++) {

        var node = nodes[i];

        if (node.href == undefined) {
            continue;
        }

        node.href = node.href.replace(new RegExp(search, 'g'), replace);
    }

}

Your usage may be something like...

replaceHrefAttributes(document.body, 'www.spam.com', 'www.rapidshare.com');

If these URLs are inline text...

You could iterate over all text nodes, using replace() to replace any string with another.

Here is a general purpose recursive function I've written to do this...

var replaceText = function replaceText(element, search, replace) {
    var nodes = element.childNodes;

    for (var i = 0, length = nodes.length; i < length; i++) {

        var node = nodes[i];

        if (node.childNodes.length) {
            replaceText(node, search, replace);
            continue;
        }

        if (node.nodeType != 3) {
            continue;
        }

        node.data = node.data.replace(new RegExp(search, 'g'), replace);
    }

}

Your usage may be something like...

replaceText(document.body, 'www.spam.com', 'www.rapidshare.com');

If you are curious as to how the code works, here is a brief explanation...

  1. Get all child nodes of the element. This will get text nodes and elements.
  2. Iterate over all of them.
  3. If this node has child nodes of its own, call the function again with element as the element in the loop. continue because we can't modify this as it is not a text node.
  4. If this node's nodeType property is not 3 (i.e. a text node), then continue as again we can't modify any text.
  5. We are confident this is a text node now, so replace the text.

You could make this function more flexible by passing search straight to it, allowing it to search for text using a string or a regular expression.

Sign up to request clarification or add additional context in comments.

7 Comments

Looks good, except that I think OP wants to replace hrefs of links. ;o) (Pretty sure anyway.)
@patrickdw: I think you may be correct, cheers. I'll modify :)
Hmmm... Looking at this if (node.href != undefined) { continue; }, and I'm thinking you meant == undefined. But then I am getting sleepy...
The continue seems redundant, why not make the test node.nodeType == 3 and do the processing in the block?
great answer! can this be used for greasemonkey userscript?
|
0
var a = document.getElementsByTagName('a');
for (var i = 0, len = a.length ; i < len ; i += 1) {
    a.firstChild.nodeValue = a.href;
}

1 Comment

You are assuming that all A elements have a firstChild and an href property. They may not. A better strategy would be to use the document.links collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.