How to execute JavaScript code


  • Admin

    A lot of people mistake the ExecuteJS block for a builtin js interpreter.
    That block, being on the bottom part of the Add Block window, belongs to the set of blocks that control a Selenium-driven browser.

    If you instead wish to purely execute javascript using the builtin interpreter Jint you can do it like this:

    BEGIN SCRIPT JavaScript
    var a = 1+2;
    var b = 'hello';
    END SCRIPT -> VARS "a,b"
    
    • All the SINGLE and LIST variables you declared inside the OpenBullet scope will be automatically declared and initialized inside the JavaScript scope.
      NOTE: only the variables with legal names according to the js standard are declared (variables must not start with a number, must not contain spaces etc.)

    • The variables you extract using END SCRIPT -> VARS "a,b" will be initialized and declared in the OpenBullet scope, so you can have a full 2-way communication vector between OpenBullet and Jint.

    If you want to read more about this you can open the LoliScript documentation
    ef21371e-e0f7-4a37-af28-6fb1a6572b3f-image.png
    and select Flow Control -> Begin (end) Script
    012d82fa-30d4-4a19-a0d8-ea5b587eea38-image.png

    Bonus: You can also use another scripting language, IronPython, which allows you to work with .NET assemblies using a pythonic syntax.



  • BEGIN SCRIPT JavaScript
    function getAlert() {
      // get all scripts
      var elem = document.scripts;
      // loop and check
      for (var i = 0, len = elem.length; i < len; i++) {
        var txt = elem[i].textContent.match(/alert\(['"]([^'"]+)['"]\)/);
        if (txt) { return txt[1]; } // if matched, return the alert text and stop
      }
    }
    var alert = getAlert();
    END SCRIPT -> VARS "alert"
    

    Will this work in order to get the alerts text? And how can I use the variable?


  • Admin

    Hey, in order to talk to the browser when using selenium you have to use the EXECUTEJS block.


Log in to reply