Execute JavaScript in FrontMost Web Browser @Example @Tool

###MACRO:   @Web Execute JavaScript in FrontMost Browser @Example @Tool

~~~ VER: 1.1    2017-04-22 ~~~

####DOWNLOAD:
@Web Execute JavaScript in FrontMost Browser @Example @Tool.kmmacros (62 KB)


###Use Cases

  1. Extract or Set Web Page Data easily
  • Paste JavaScript into KM Macro Action
  • Use as a tool to execute any JavaScript file.
  • This could allow you to build a library of JavaScript files that can be easily run in either Browser using only one Macro. Of course you may want to modify the Macro to do other things with the results.
  • These scripts provide a number of statements and functions you may find useful in your other JavaScript and JXA scripts.

###ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Make it Easy to Execute a JavaScript in EITHER Safari or Chrome
  • Enter the JavaScript in one place, that is used by both
    • The script determines which Browser is FrontMost and uses the appropriate scripting terms for that Browser.
  • Execute the JavaScript from either:
    • KM Variable
    • Choose File
  • Provide an Error Handler for both JIB and JXA Scripts
  • Provide an example of using a JXA script to
    • Execute scripts in Browswers
    • Get Web Page Info without JIB

HOW TO USE:

  1. Set Source of JavaScript
  • In the Action "ENTER Source of JavaScript (FILE or TEXT)"
  • You could easily add a Prompt for User Input Action to select this.
  1. Open the Web Page of Interest in Safari or Chrome
  2. Trigger this Macro
  3. Results are displayed and put on Clipboard

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Maco, not me. :wink:
      .
  • Assign a Trigger to this maro. I prefer to Trigger by Name.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • Enable this Macro.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magneta color)
    • Action: "ENTER Source of JavaScript (FILE or TEXT)"
      • Enter "FILE" to choose JavaScript from file
      • Enter "TEXT" to use the JavaScript in the below Action.
    • Action: "ENTER Your JavaScript Below (to be run in FrontMost Browser)"
    • OPTION: If you choose to change the name of the KM Variable used in the Execute JXA Script, then you must also change it in the Script Error Handler:
      • Action: CHANGE Variable in "to:" Block to Variable You Used Above

TAGS: @JavaScript @JIB @Safari @Chrome @JXA @Web

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

ACTION COLOR CODES

  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Varibles or Clipboards,
    OR IF/THEN and PAUSE Actions

REQUIRES:

  1. Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
  2. El Capitan 10.11.6+
  • It make work with Yosemite, but I make no guarantees.

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge will do no hard, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

4 Likes

The link appears to be invalid.

Please try again. I just tested it and it works for me. Just in case, here it is:
https://forum.keyboardmaestro.com/uploads/default/original/2X/4/4e5c3f359eb65e9a0784ba371d47324904497422.kmmacros

Should have sent that privately instead of posting. Sorry.

Ran into this on some other pages of yours. Took a guess and logged out and backed in, and the problem went away. Never had to do that before.

I have not had any other complaints about broken links, nor have I seen any with others. First attempted fix is to restart your browser and/or try other browsers.

The KM Macro download URLs are system generated, and are very unlikely to be invalid.