Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Assistance with regular expressions

    on page load, how would i check the entire body for a string and if that string was found, use alert() to notify the user?

    thanks
    IF I DID GOOD THROW ME A REP POINT

  • #2
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts
    See no reason why woud you do do that, as the stuff comes from server you know it before it loads. Use server side scripting.


    Otherwise:

    match function
    and document.innerHTML

    this wont work on ie6 coz there is a size limit for innerHTML, don't know how would you do it there, unless parsing whole doc via DOM

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ill give it a go... im trying to throw a small grease monkey script together to alert me whenever something is within the content of a page im on

    thanks
    IF I DID GOOD THROW ME A REP POINT

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Try this:-

    Code:
    <script type="text/javascript" >
    
    var str2find = "P~hi~li~p"
    // Note: str2find must be munged initially with ~ characters as otherwise the string will always be found!
    str2find = str2find.replace(/~/g,"");  // unmunge
    
    window.onload=function(){
    var str = document.getElementsByTagName('html')[0].innerHTML;
    var a = str.indexOf(str2find);
    if (a >= 0) {
    alert ("The HTML contains the string " + str2find);
    }
    else {
    alert ("Sorry - this sequence of characters was not found.");
    }
    }
    </script>

    "In the beginner's mind there are many possibilities, but in the expert's mind there are few.” Shunryu Suzuki (Japanese Zen priest, ?-1971)

  • #5
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks phil.. it appears as if the window.onload isntfiring:

    Code:
    var str2find = "~secret~"
    str2find = str2find.replace(/~/g,"");  // unmunge
    
    window.onload=function()
    {	
    	alert("MADE IT");
    	var str = document.getElementsByTagName('body')[0].innerHTML;
    	var a = str.indexOf(str2find);
    	if (a >= 0) 
    	{
    		alert ("The HTML contains the string " + str2find);
    	}
    	else 
    	{
    		alert ("Sorry - this sequence of characters was not found.");
    	}
    }
    I never get the alert "MADE IT"
    IF I DID GOOD THROW ME A REP POINT

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by scrupul0us View Post
    thanks phil.. it appears as if the window.onload isntfiring:

    Code:
    var str2find = "~secret~"
    str2find = str2find.replace(/~/g,"");  // unmunge
    
    window.onload=function()
    {	
    	alert("MADE IT");
    	var str = document.getElementsByTagName('body')[0].innerHTML;
    	var a = str.indexOf(str2find);
    	if (a >= 0) 
    	{
    		alert ("The HTML contains the string " + str2find);
    	}
    	else 
    	{
    		alert ("Sorry - this sequence of characters was not found.");
    	}
    }
    I never get the alert "MADE IT"
    No idea why. Of course, you cannot inject the script into some external web page in another domain. The best you can do is to View Source, and then capture the code and then search for the character sequence in question. You could do that with Notepad.


    var str2find = "~secret~" is not properly munged as the sequence secret necessarily appears on the page - in this line. You must do something like
    var str2find = "se~cr~et";that is, place the tildes within the string.

  • #7
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    @phil: its a greasemonkey script for firefox... its not really injected anywhere as its run on my local machine... the string im looking for is just one word so doing the view source etc... doesnt really apply

    never-the-less the issue seems to be that the onload isnt firing

    @cranford: im reviewing
    IF I DID GOOD THROW ME A REP POINT


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •