Can I use JavaScript/JQuery in SoundFlow to control my browser?
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!!
- Christian Scheuer @chrscheuer2021-10-06 20:32:28.143Z
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'); `);
Christian Scheuer @chrscheuer2021-10-06 20:34:03.756Z
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"); `);
Mike Wax @mikewax
So
sf.appleScript
will still run JavaScript?Or should i use something like
function addIVR(script) { sf.system.execJXA({ script: "document.getElementById('AddNewIVR').submit()" }); }
Christian Scheuer @chrscheuer2021-10-06 20:47:09.465Z
Yes, forget about the fact that it mentions appleScript in this. JXA would execute something very different, don't use that.
Mike Wax @mikewax
Copy that!
- In reply tochrscheuer⬆:
Christian Scheuer @chrscheuer2021-10-06 20:51:20.811Z
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⬆:
Christian Scheuer @chrscheuer2021-10-06 20:38:52.468Z
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() `);
Mike Wax @mikewax
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?
Christian Scheuer @chrscheuer2021-10-06 20:47:59.319Z
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.querySelectorAllChristian Scheuer @chrscheuer2021-10-06 20:49:03.781Z
Essentially,
$('.button').click();
in jQuery, would be
document.querySelector('.button').click()
in modern browsers
- In reply tochrscheuer⬆:
Mike Wax @mikewax
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....
Christian Scheuer @chrscheuer2021-10-06 20:56:22.366Z
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.
Christian Scheuer @chrscheuer2021-10-06 20:57:25.289Z
If you're getting errors it's possible you're passing it invalid javascript
- In reply tochrscheuer⬆:
Mike Wax @mikewax
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⬆:
Christian Scheuer @chrscheuer2021-10-06 20:58:55.885Z
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.
Mike Wax @mikewax
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!Christian Scheuer @chrscheuer2021-10-06 21:13:42.619Z
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 :)Mike Wax @mikewax
Very much appreciated!
I'm using a work computer so everything is heavily monitored as is...HAHA!
But always good to know!