User Tools

Site Tools


actions:Execute_a_JavaScript_in_Browser

Execute JavaScript in Browser Actions

There are several Execute a JavaScript in Browser actions that let you execute a JavaScript in a web browser page:

  • Execute a JavaScript in Custom HTML Prompt
  • Execute a JavaScript in Front Browser
    • The “Front Browser” is whichever of the following is currently frontmost, or was the last web browser that was frontmost:
      • Google Chrome
      • Safari
      • Chrome-based Browsers like Brave Browser
  • Execute a JavaScript in Google Chrome
  • Execute a JavaScript in Safari

Front Browser supports these bundle IDs:

  • com.apple.Safari
  • com.apple.SafariTechnologyPreview
  • com.google.Chrome
  • com.google.Chrome.canary
  • com.brave.Browser
  • com.vivaldi.Vivaldi

All of these Actions work the same. The difference is in which Web Browser the JavaScript will be executed.

This action allows you to execute a specified JavaScript, either from a file or text entered in the Action itself, and return the results.

Options for Returning Results

  • ignore results.
  • display results in a window.
  • display results briefly.
  • display results large.
  • type results.
  • paste results.
  • save results to variable.
  • save results to the system clipboard or a named clipboard.
  • asynchronously — the action runs while the macro continues on, no results are returned.

If the results are to be saved to a variable or a clipboard then further text areas appear in the action allowing you to specify the variable or specific clipboard.

Using Keyboard Maestro Variables

JavaScript in browsers can only GET Keyboard Maestro variables. They cannot be set. If your Keyboard Maestro Variable contains spaces, they must be replaced with underscores in the JavaScript, for example:

snippet.javascript
// Get Keyboard Maestro variable "My KM Data"
var myData = document.kmvar.My_KM_Data

Saving Results to Keyboard Maestro Variables

If you need to set a Keyboard Maestro Variable from the JavaScript in a browser, then use the JavaScript return value to return the data to the Keyboard Maestro Action, which can be set to save the results as a Keyboard Maestro Variable.

If you need to set more than one Keyboard Maestro Variable, one approach is to return the data for each variable on a separate line (or with delimiters of your choice), and then parse in a following Search using Regular Expression.

For example:

Execute a JavaScript in Custom HTML Prompt

For the Execute a JavaScript in Custom HTML Prompt action, you can specify a Custom_HTML_Prompt with a matching data-kmwindowid to apply the JavaScript to, otherwise it will be applied to all open Custom HTML Prompt windows.

The JavaScript can access variables by using the window.KeyboardMaestro functions described in the Custom HTML Prompt action.

See Also

Actions

Forum

actions/Execute_a_JavaScript_in_Browser.txt · Last modified: 2021/03/27 23:55 by JMichaelTX