No internet connection
  1. Home
  2. How to

Can I use JavaScript/JQuery in SoundFlow to control my browser?

By Mike Wax @mikewax
    2021-10-06 19:29:05.246Z

    Hello!

    Probably a silly question, but am I able to write Vanilla JavaScript or JQuery in SoundFlow just as is? Or do I need anything in SoundFlow to initiate the scripts? Like sf.RunMyJavascript{} or does it have to be inside a function RunMyScript()?

    EX: can I use:

    document.getElementById('button').click()
    

    or

    $(document).ready(function () {
       $('.button').click();
    }); 
    

    Basically I'm trying to write a JavaScript/JQuery function that will click a specific button on a website to upload files for client review.

    Thank you for the help!!

    • 18 replies
    1. You'll need something like this:

      
      var chrome = sf.ui.app('com.google.Chrome');
      
      function executeJs(script) {
          var res = sf.system.execAppleScript({
              script: 'tell application "Google Chrome"\n' +
                  'set str to execute in active tab of the front window JavaScript "' + script.replace(/"/g, '\\"') + '"\n' +
                  'str\n' +
                  'end tell\n'
          });
          if (res.standardError) {
              log(res.standardError);
              throw 0;
          }
          //log(res.result);
          return res.result;
      }
      
      executeJs(`
      alert('I am now on the webpage');
      `);
      
      1. Actually, now I think of it, it can be made even simpler like this:

        sf.appleScript.googleChrome.windows.getByIndex(1).activeTab.execute(`
        alert("Test 1-2-3");
        `);
        
        1. Mike Wax @mikewax
            2021-10-06 20:38:38.450Z

            So sf.appleScript will still run JavaScript?

            Or should i use something like

            function addIVR(script) {
                sf.system.execJXA({
                    script: "document.getElementById('AddNewIVR').submit()"
                });
            }
            
            1. Yes, forget about the fact that it mentions appleScript in this. JXA would execute something very different, don't use that.

              1. Mike Wax @mikewax
                  2021-10-06 20:48:12.873Z

                  Copy that!

                  1. In reply tochrscheuer:

                    The reason being:

                    We're using AppleScript / Apple Events to tell Chrome to run some Javascript in its browser tab.

                    execJXA is directly running "Javascript for Applications" which is something completely different. It's essentially a sort of enhanced Javascript that allows to script the OS, but it's extremely poorly documented and is not widely used or actively developed. So I would highly recommend to never use JXA. Also, it wouldn't get you closer to running JS in your browser, so definitely don't go down this path.

                • In reply tochrscheuer:

                  But yea you can do all sorts of cool things with this. This clicks the last reply button on the forum :)

                  sf.appleScript.googleChrome.windows.getByIndex(1).activeTab.execute(`
                  Array.from(document.querySelectorAll('a.dw-a.dw-a-reply.icon-reply')).slice(-1)[0].click()
                  `);
                  
                  1. Mike Wax @mikewax
                      2021-10-06 20:43:39.147Z

                      That's awesome!

                      Ok, then i just need to figure out how to click on a specific button on the active webpage.

                      Will this also work for JQuery?

                      1. There's no need to use jQuery if you use document.querySelector or document.querySelectorAll. jQuery would only work if the website you're visiting is using jQuery themselves.
                        Check Mozillas docs for document.querySelector or document.querySelectorAll

                        1. Essentially,
                          $('.button').click(); in jQuery, would be
                          document.querySelector('.button').click() in modern browsers

                      2. In reply tochrscheuer:
                        Mike Wax @mikewax
                          2021-10-06 20:55:12.783Z

                          Can this stand alone or does it still need to be inside a function. I'm getting some funky errors when i try to run it.

                          Error in Apple Event: 12....

                          1. I would recommend using my first answer with the executeJs function as its error handling may be slightly better.

                            Remember it needs you to have Google Chrome open and a tab open.

                            1. If you're getting errors it's possible you're passing it invalid javascript

                              1. In reply tochrscheuer:
                                Mike Wax @mikewax
                                  2021-10-06 20:59:05.476Z

                                  Ok, great! I will give that a shot.

                                  Thanks for your patience with me! I will report back once I have something setup (chances are i will have more questions...LOL).

                                • In reply tomikewax:

                                  Yes, this

                                  sf.appleScript.googleChrome.windows.getByIndex(1).activeTab.execute(`
                                  Array.from(document.querySelectorAll('a.dw-a.dw-a-reply.icon-reply')).slice(-1)[0].click()
                                  `);
                                  

                                  can stand alone in a script. It works fine for me here at least.

                                  Since all the solutions here use apple script or apple events there's the OS' security sandbox to consider. If you're seeing errors, it's possible you need to allow SF to control Chrome in the Security & Privacy settings.

                                  1. Mike Wax @mikewax
                                      2021-10-06 21:03:26.004Z

                                      As always, right you were. I needed to allow JavaScript from Apple Events in Chrome.!

                                      Perfect! I will get to coding and see what i can come up with and report back as to how it went.
                                      Thank you!

                                      1. Great :) Please remember the implications of doing this. It essentially allows any app running on your computer to read & control everything going on in your browser.
                                        It's probably fine, just want to make sure you (and anybody else reading this in the future) are aware of how broad access is turned on with that switch :)

                                        1. Mike Wax @mikewax
                                            2021-10-06 21:20:01.489Z

                                            Very much appreciated!

                                            I'm using a work computer so everything is heavily monitored as is...HAHA!
                                            But always good to know!