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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool GM, not getting past if statement

    So I the following code is my greasemonkey script:
    Code:
    var msg = document.body.innerHTML;
    
    if(msg.match(/Big monster/gi).length > 0)
       {
       alert("attack");
       //Do stuff
       }
    alert("got past");
    if(msg.match(/Big monster/gi).length == 0)
       {
       alert("not attack");
       //Do other stuff
       }
    When the first condition is true, the code inside executes perfectly. Otherwise, nothing is getting run. Note that the same thing happens if I use an "else" instead of the second "if". Note too, if the first condition is true, then "got past" gets displayed, but if it's false, then even that line doesn't get executed.

    This is my first time with javascript, and also my first time messing with Greasemonkey, so is there something obvious I'm missing here?

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by scnerd View Post
    but if it's false, then even that line doesn't get executed.
    Which indicates that you get an error, for example something is undefined. Use the console to verify what happens.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    The length of the value returned will never be zero. When no matches are found the length will be 1.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    @ felgall

    That wouldn't explain the "got past" not being displayed. And it's actually not correct.

    If the regular expression includes the g flag, the method returns an Array containing all matches. If there were no matches, the method returns null.
    https://developer.mozilla.org/en-US/...s/String/match

    And as we hopefully all know: null doesn't have a property 'length', thus erroring out.

  • Users who have thanked Airblader for this post:

    scnerd (01-31-2013)

  • #5
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Airblader View Post
    Which indicates that you get an error, for example something is undefined. Use the console to verify what happens.
    Right, I got that there was an error, but I'm literally running the code above. Besides the conditions and the "alert" calls, I'm actually doing nothing (everything else is commented out), and the behavior is the same. When the condition is met, everything runs, else nothing is run. So my question is more: is there any reason why that first condition being false should mean that the rest of the code doesn't run?

    Here, for reference, I am running this *exact* code (this is the entire script):
    Code:
    var msg = document.body.innerHTML;
    
    if(msg.match(/Big monster/gi).length > 0)
       {
       alert("attack");
       //Do stuff
       }
    else
       {
       alert("not attack");
       //Do other stuff
       }
    And the behavior is as I said above. Either "attack" gets displayed, or nothing does.

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    There is a difference between a condition being false and getting an error.

  • #7
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Airblader View Post
    @ felgall

    That wouldn't explain the "got past" not being displayed. And it's actually not correct.



    https://developer.mozilla.org/en-US/...s/String/match

    And as we hopefully all know: null doesn't have a property 'length', thus erroring out.
    Thanks, I'd thought that it'd return an empty array, not just null.

  • #8
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    For future reference you should not just learn what match returns from this, more important is to learn that simply looking at the console would've told you exactly the same.

  • #9
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Airblader View Post
    For future reference you should not just learn what match returns from this, more important is to learn that simply looking at the console would've told you exactly the same.
    For future reference, working in GM, where can I get said console? In firefox, does the Web Dev -> Web Console do what you're talking about? Or does GM have its own debugging console? Thnx

  • #10
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    I have never worked with GM before, so I don't know if it shows up in the console log. But even if not, just do what I did: Take your little code snippet and put it in a service like jsfiddle or writecodeonline.com.

    I'd just assume that there's gonna be some sort of logging even with GM, though.


  •  

    Tags for this Thread

    Posting Permissions

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