Issue with JavaScript in Chrome Action & KM Variables

Issue with JavaScript in Chrome Action & KM Variables

This simple JavaScript FAILS when run in a Execute_a_JavaScript_in_Google_Chrome action (KM Wiki), which I will refer to as JIC:

JavaScript

try {
  var scriptResults = document.title;
}
catch (oErr) {
  scriptResults = oErr.message;
}
console.log (scriptResults);
scriptResults;

Actually, at this point, ALL of my KM Macros with JIC would fail.
All of my scripts and test macro are posted at the bottom of this post.

Background

This is a very strange, unlikely issue caused by the interaction among:

  • Chrome Browser JavaScript Compiler
  • KM JavaScript in Chrome Action
  • KM Variable containing unusual characters

The result is a failure, without any error msg, of all KM JavaScript in Chrome Actions. The script/action returns nothing, an empty string (""). So you do not have any immediate clues as to why the JIC Action failed, that had been working fine previously for a long time.

Clues

  1. The same script ran using AppleScript to inject the JavaScript into Chrome worked fine, both running the AppleScript from Script Debugger and from a KM Execute AppleScript Action. (see below).
  • By viewing the Chrome JavaScript debugger/console when I ran the KM Macro with the JIC Action. It showed me this:

Although this is shown in the Chrome JavaScript Console, it is NOT returned as an error msg.

Suspected Cause

I'll give you the suspected cause now, and then provide more details in my investigation.
Cause: Strange, unusual Characters in the KM Variable "SD_Copy"
The Characters:
\x0A and maybe \x22

But why, you ask?

Clues from the Chrome JavaScript Console

I'm still a novice at using the JavaScript Console, so I didn't notice or pay attention to some other information being displayed at the end of the line with the "Uncaught SyntaxError" msg:

It looked like a link, so I clicked on it (not knowing what it would do), and it revealed much:

But that line is NOT in my script!!! Where did it come from???

Evidently the KM JIC Action automatically initializes the JavaScript session with ALL of your KM Variables, whether or not they are used in your JIC script. @peternlewis, please correct/clarify this if I'm wrong.

I'm not using that KM Variable, "SD_Copy", or any KM Variable in my simple script. So, my immediate reaction is to delete that KM Variable.

Deleting the offending KM Variable Fixed the Problem

When I ran my KM Macro with the JIC Action again, it worked perfectly without error.

This is a strong motivator to delete your KM Variables used in a Macro (at the end of the Macro) if you will no longer need them.

What's the Long Term Solution?

Taking the advice of "an ounce of prevention is worth a pound of cure", I'd like to

Make this KM Feature Request to @peternlewis:
Load ONLY the KM Variables used in the script of a JIC (& JIS) Action into the JavaScript Session.
The same would apply, of course, to the JavaScript in Safari (JIS) Action.

More Suggestions

I don't know what is possible or feasible here, so I'll just throw out these ideas.

  • Can the KME trap the JavaScript syntax error shown in the Console, and report it back?
  • If not, can the KME filter out the offending characters?

More Testing

Another big clue was when I ran the same script/macro on another Mac, and it worked fine.

So, I came back to my new iMac-27, with a focus on the Chrome Browser. I disable all Chrome extensions, but that still did not fix the problem. I rebooted several times, and launched only LaunchBar, KME, and Chrome. Still got the same error.

I was about ready to boot into Mac Safe Mode, when I took another look at the Chrome JavaScript Console. That's when I finally noticed the link to the offending line:


### AppleScript Injection of JavaScript into Chrome
```applescript

set jsStr to "
try {
  var scriptResults = document.title;
}
catch (oErr) {
  scriptResults = oErr.message;
}
console.log (scriptResults);
scriptResults;
"

tell application "Google Chrome" to activate

set scriptResults to my doJavaScript(jsStr)

return scriptResults

--~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~

on doJavaScript(pScriptStr)
  
  local jsScriptResults, frontAppName
  
  try
    
    set jsScriptResults to "TBD"
    
    tell application "Google Chrome"
      set jsScriptResults to execute (active tab of window 1) javascript pScriptStr
    end tell
    
  on error e
    set jsScriptResults to "Error in handler doJavaScript()" & return & return & e
    
  end try
  
  return jsScriptResults
  
end doJavaScript
```

### MACRO:   @Web @TEST @Debug @JavaScript in @Chrome

~~~ VER: 1.0    2017-05-22 ~~~

#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/a/a4120378c4abf61e7d4c567ebaacf6f1130cf0a3.kmmacros">@Web @TEST @Debug @JavaScript in @Chrome.kmmacros</a> (5.2 KB)

---

### ReleaseNotes

TBD

---

<img src="/uploads/default/original/2X/6/6003df59c00cc2693aca9efb726efa6e4f6ca309.png" width="459" height="1089">

That is correct. The document.kmvar field is populated with your variables, just like the environment variables of an Execute Script action.

This is impossible. It is a subset of the halting problem and would require executing the script in order to determine which variables are accessed.

The characters are filtered, that is why they are \0x0A.

Clearly there is some issue with the filtering, or alternatively perhaps some bug in Chrome that is misinterpreting the text itself. If you send me the exact value of the variable (preferable the Keyboard Maestro Variables.plist file containing the variable and the name of the variable) then I can see what it is not liking and probably fix it (or indicate that it is a Chrome bug, though that is less likely.