Jebzaki
Jebzaki

Reputation: 163

Looking for a way to search an html page with javascript

what I would like to do is to the html page for a specific string and read in a certain amount of characters after it and present those characters in an anchor tag.

the problem I'm having is figuring out how to search the page for a string everything I've found relates to by tag or id. Also hoping to make it a greasemonkey script for my personal use.

function createlinks(srchstart,srchend){
    var page = document.getElementsByTagName('html')[0].innerHTML;

    page = page.substring(srchstart,srchend);

    if (page.search("file','http:") != -1)
    {
        var begin = page.search("file','http:") + 7;
        var end = begin + 79;

        var link = page.substring(begin,end);
        document.body.innerHTML += '<a href="'+link+'">LINK</a> | ';

        createlinks(end+1,page.length);
    }       
};

what I came up with unfortunately after finding the links it loops over the document again

Upvotes: 0

Views: 1902

Answers (2)

vol7ron
vol7ron

Reputation: 42099

Assisted Direction


  1. Lookup JavaScript Regex.
  2. Apply your regex to the page's HTML (see below).

Different regex functions do different things. You could search the document for the string, as suggested, but you'd have to do it recursively, since the string you're searching for may be listed in multiple places.


To Get the Text in the Page


  • JavaScript: document.getElementsByTagName('html')[0].innerHTML
  • jQuery: $('html').html()

Note:

  1. IE may require the element to be capitalized (eg 'HTML') - I forget
  2. Also, the document may have newline characters \n that might want to take out, since one could be between the string you're looking for.

Upvotes: 1

Charlie Martin
Charlie Martin

Reputation: 112366

Okay, so in javascript you've got the whole document in the DOM tree. You an search for your string by recursively searching the DOM for the string you want. This is striaghtforward; I'll put in pseudocode because you want to think about what libraries (if any) you're using.

function search(node, string):
    if node.innerHTML contains string
       -- then you found it
    else
       for each child node child of node
           search(child,string)
       rof
    fi

Upvotes: 0

Related Questions