Execute JavaScript in Browser Actions – Best Practices

Alternatively, if you:

  • don't want a document.kmvar reference to be created in the website's DOM model at all, or
  • you are using multiple actions, launched at different moments, on the same page, and there is any risk of a value needed by one action having been changed by another,

and assuming that you do need run-time access, within your JS, to the values of Keyboard Maestro variable names, then you can bypass the use of the Execute a JS in Browser actions entirely, and run your code, with a private copy of the kmvar names and values for each action, inside a non-global JavaScript context to which other scripts have no access.

Here is one approach (wrapping the user JS in a private context, and executing it in the browser, with access to a private and temporary kmvar object, from JavaScript for Automation):

Examples for Safari and Chrome:

Safe JS in Safari and Chrome Macros.kmmacros (25.1 KB)

2 Likes