1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to search a website for specific word using JavaScript?

Discussion in 'JavaScript' started by Dr.Weed, Apr 9, 2015.

  1. #1
    I'm creating an extension for Chrome which will search for a certain phrase/word on the website. How can I do this with JavaScript? I tried many solutions I found on the Internet, but none of them works.

    Code: https://www.dropbox.com/sh/0d38od48qhpdhhf/AAD7sYee6KDa5jUD1TIgg0d4a?dl=0
     
    Dr.Weed, Apr 9, 2015 IP
  2. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #2
    https://jsfiddle.net/hm659mkd/1/


    <div id="test"><h1>Some Stuff</h1></div>
    <div class="woot woory what">ok what now?</div>
    <div id="ok">findme i bet you cant!</div>
    HTML:

    var word = "findme",
        queue = [document.body],
        curr
    ;
    while (curr = queue.pop()) {
        if (!curr.textContent.match(word)) continue;
        for (var i = 0; i < curr.childNodes.length; ++i) {
            switch (curr.childNodes[i].nodeType) {
                case Node.TEXT_NODE :
                    if (curr.childNodes[i].textContent.match(word)) {
                        alert("Found!");
                    }
                    break;
                case Node.ELEMENT_NODE :
                    queue.push(curr.childNodes[i]);
                    break;
            }
        }
    }
    Code (JavaScript):
     
    HowDoYou, Apr 16, 2015 IP
  3. imbrod

    imbrod Well-Known Member

    Messages:
    212
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    I would guess you'll need node.js to do that, not client side javascript.
     
    imbrod, Apr 16, 2015 IP