Returning a result string from `Execute JavaScript in Safari`

I tried like above after reading docs

This returns nothing

No window gets shown with result

If I run it in console I do get result though

eval(3+2) this works

so its something about eval(document.activeElement) that's wrong, I don't get what though

Not really sure how your action relates to any form fields but you might Try:


More likely, though, you should study the example code here: The activeElement Read-Only Property of the Document Interface Returns the Element Within the DOM That Currently Has Focus

A KM result is a string.

What string would you expect from that expression ?

The JS value of applying eval to that slightly unexpected argument is just undefined which leads to an empty string.

(eval – generally deprecated anyway – expects a string argument – JS source code – not a reference to a DOM object)

There are various string values which you could derive from a reference to the document.activeElement object.

For example document.activeElement.outerHTML

This didn't work for me, nothing showed.

I also tried it with: console.log(window.location.href) also nothing showed.

Running above action did nothing too.

If I do paste it in dev tools console though I would see a string:

Not sure what I am doing wrong.

Ok I found the issue. It's a bug with KM.

It only works with Safari it seems. In Safari Technology Preview, nothing gets shown if I run

When Safari Technology Preview window is active. However KM docs says that it should support Safari Technology Preview.

Perhaps @peternlewis can take a look :heart:

I need above action to work in Safari Tech Preview, not regular Safari.

From actions:Execute a JavaScript in Browser [Keyboard Maestro Wiki]

Is why I think it's a bug. I can replicate it 100 % of times on latest macOS version.

My Safari Tech Preview version:

Not surprised. It's somewhat disembowled.

But a Custom HTML Prompt isn't interactive, so I don't know what you're trying to accomplish.

You'd have to be using a JavaScript function called with an onchange for each field of the form that would report the tag name to you. But what would you do with that?

As you can see, I'm confused about what you want here.

I want to do this

So instead of the applescript, I run the Execute JavaScript.

But beyond that, I want to also run some other javascript stuff inside Safari Tech Preview.

But due to bug, that whole action does not return anything for that app.

One of the things for example that I wanted to do. Is using document.activeElement know if I am currently focused in on some input DOM element.

If yes, do an action. If not, do another action.

This can only be possible with Execute JS in Front Browser action. Just need to make it work with Safari Tech Preview.

Hope @peternlewis can take a look at it soon.

That's still very vague for any concrete code.

But this doesn't seem to be Keyboard Maestro territory because it's interacting with the HTML. So it's a JavaScript project. Except you're using HTML you have no control over (a page from the Web, that is).

So still confused here and hopig I've confused you a bit, too :grinning:.

But this doesn't seem to be Keyboard Maestro territory because it's interacting with the HTML

I want to interact with HTML from KM though. KM provides an action for that.

I can do everything I need, it's just I need it done in Safari Tech Preview browser. Which currently breaks. It's a bug though as in docs it says this browser should be supported.

You can do a lot of things using JS. Once the bug is fixed, I'll share my solution in the forum too. :slight_smile:

I don't have Safari Technology Preview, so I can't look at that I'm afraid.

I would check that you have both Automation control of STP and JavaScript over Apple Events enabled in STP.

Do other Front Browser actions work with STP - if some do, then you probably have Automation control.

What if you use different JavaScript, and just say


do you get that back?

Maybe there is some additional security permission required in STP.

I did not have JavaScript over Apple Events enabled enabled. After enabling it, it works.