Bug? Execute JavaScript in Browser

,

I have a simple JavaScript, which I use to get the HTML of an element in Safari.

It worked fine before version 8 of Keyboard Maestro, but now it does not return anything.

This is the JavaScript:

 var elemList = document.getElementById("JobTicketLineItems");
 elemList.innerHTML;

It works in both Chrome and Safari if I paste the command into the console.

The console gives an error when I try the action in KM.

Safari error:

Chrome error:

Keyboard Maestro Actions.kmactions (1.4 KB)

Given that, then the issue is NOT likely to be with the JavaScript code.
I remember getting a similar error some time ago when I had a KM variable that contained some strange unicode values. Once I deleted that variable, the error went away.

What may not be obvious, is that when you use a Execute JavaScript in Browser action, the KM Engine inserts ALL of your KM Variables in your KM app. Not just the variables used by the script, but every variable that you can see in the KM Preferences > Variables tab.

You can try this to identify the KM variable causing the issue:

  1. On the web page of interest, before you run the KM macro, right-click on the object of interest, and select "Inspect"
  • This will open the Chrome Dev Tools, with the JavaScript Console.
  • Click back in the main web page, and run your macro
  • You should see an error in the JavaScript Console, with a link to the line causing the issue. Something like this:
    .

You may have to do some exploring in the Chrome Dev Tools window, but hopefully that will lead you to the cause.

2 Likes

Thanks. The trick with clicking the error in the Google Chrome inspector found the offending variable.

This was the highlighted error in Chrome:

document.kmvar.urlList = "http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html\x0Ahttp://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html\x0Ahttp://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html ";

I can not see what should make this block the execution in Chrome and Safari of the Javascript. But it does.

I deleted this variable in the preferences of KM and now I can run the Javascript from KM.

Thanks for the help.

1 Like

Hmm, sounds like an issue with the encoding of the variable.

I could not duplicate it unfortunately, and I’d like to. Do you know what the variable was set from?

If you happen to have a backup of the Keyboard Maestro Variables.sqlite file containing the broken variable, that would be ideal, or if you can recreate it somehow, that would work too.

I can find the Keyboard Maestro Variables.sqlite from my backup.

Can I somehow open it to verify it contains the offending variable?

Due to the nature of the file I will email it to your support email.

Not really. You could quit Keyboard Maestro Engine and Keyboard Maestro, replace the one in your preferences folder temporarily, launch them and verify the failure.

Absolutely.

Thanks.

The files have been send and I can confirm that the issue happens again with the restored variables.

1 Like

Thanks @JimmyHartington. Apparently the variable had an invisible U+2028 LINE SEPARATOR character in it, and apparently Chrome did not like that.

I have extended the encoding for all non-ASCII characters, seems extreme but there is no way to know whatever characters Chrome might not like in a string.

Fixed for the next version.

1 Like

Nice catch by you.

Will the same apply to Safari? Because it errors there too.

1 Like

Yes, it applies to both. I didn’t check Safari, but I applied the patch to both.

I was looking everywhere for how to get "Execute JavaScript in Google Chrome" working. Here is the only solution that worked for me:

In Chrome, View > Developer > Allow JavaScript from Apple Events

Can this step go in the wiki? It's not documented anywhere.

4 Likes

I have documented it in the Assistance section of the wiki at Allow JavaScript From Apple Events, but it should also be referenced/copied in the documentation for the various actions and tokens that require it.

1 Like

This is the answer I want, thanks!