Execute Actions: JavaScript in (browser) – getting help from the debugger

Execute JavaScript in (Safari or Chrome) actions enable all kinds of useful interactions with browsers, and are perhaps a bit easier to draft than they look.

The trick is ⌘⌥I, which lets you step through your running JS code line by line, watching the variable values change, and inspecting the properties of objects, in the browser's symbolic debugger.

In either browser, you can select something on the web page, and use ⌘⌥I or "Inspect Element" in the Ctrl-click contextual menu.

(In Safari, you may first need to enable the 'Developer Menu' through Preferences > Advanced).

I personally prefer to use the Chrome tools (easier XPATH searches), but both hugely reward the effort of some patient exploration and experimentation. The debugger is really the best place to watch it all happen in slow motion, and learn how it all works.

To make the execution of your JavaScript action pause at a point of interest, allowing you to start stepping through the execution line by line, insert a line which simply consists of:

debugger;

in your code.

In the next version of OS X, you will be able to do this with JavaScript for Applications automation code from Script Editor.

https://developer.apple.com/library/prerelease/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-11.html#//apple_ref/doc/uid/TP40014508-CH110-SW1

(You can already use Script Editor to draft your Browser Javacript code, converting the function which will be used in the browser to a string, and submitting it for browser execution using the techniques shown in this post:

5 Likes

Thanks for sharing, Rob. This is very helpful.

Using the Safari JS debugger with JXA will be very cool. I only wish they would push it out for Yosemite. Maybe El Capitan will be different, but so far it takes me about a year to upgrade Mac OS after it’s release. Just too many bugs in both the OS and apps, and too many apps that are broken. As you know, I’m just now upgrading to Yosemite. :smile:

But this is all good stuff. I’m clipping it all to my Evernote account.