This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
action:Execute_a_JavaScript_in_Google_Chrome [2016/06/06 23:50] peternlewis |
action:Execute_a_JavaScript_in_Google_Chrome [2016/07/26 19:50] JMichaelTX |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | The Execute JavaScript in Safari or Google Chrome action allows you to execute a specified JavaScript, either from a file or text. For example, | + | {{:action:javascript-icon.png?nolink|}} |
+ | ====== Execute JavaScript in Browser ====== | ||
- | //alert("hello")// | ||
- | and return the results. Options for the results of a JavaScript are: | + | **The Execute JavaScript Action is available ONLY for [[action:Execute_a_JavaScript_in_Safari|Safari]], [[action:Execute_a_JavaScript_in_Google_Chrome|Google Chrome]], and, as of Ver 7.1, [[action:Execute_a_JavaScript_in_Custom_Prompt|Custom HTML Prompt]]** |
+ | |||
+ | * These are actually three separate Actions, one for each browser, and one for [[action:Custom_HTML_Prompt|Custom HTML Prompt Action]] | ||
+ | * This action allows you to execute a specified JavaScript, either from a file or text entered in the Action itself, and return the results. | ||
+ | * If you need to access other Mac apps, see [[action:Execute_a_JavaScript_For_Automation|Execute JavaScript for Automation (JXA) Action]] | ||
+ | |||
+ | {{:action:km-execute-javascript-actions.png?nolink|}} | ||
+ | |||
+ | {{:action:km-execute-javascript-chrome.png?nolink|}} | ||
+ | |||
+ | ===== Options for Returning Results ===== | ||
*ignore results. | *ignore results. | ||
Line 17: | Line 27: | ||
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. | 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. | ||
- | JavaScript can access variables by using the document.kmvar dictionary, see the Variables section. | + | ===== Using Keyboard Maestro Variables ===== |
+ | |||
+ | JavaScript can only **GET** Keyboard Maestro variables. They cannot be set: | ||
+ | |||
+ | ```javascript | ||
+ | var myData = document.kmvar.My_KM_Data | ||
+ | ``` | ||
+ | |||
+ | If your Keyboard Maestro Variable contains spaces, they must be replaced with underscores in the JavaScript.\\ | ||
+ | So, for example:\\ | ||
+ | Keyboard Maestro Variable Name: My KM Data | ||
+ | JavaScript Reference: document.kmvar.My_KM_Data | ||
+ | |||
+ | ===== Saving Results to Keyboard Maestro Variables ===== | ||
+ | |||
+ | If you need to set a Keyboard Maestro Variable from the Browser JavaScript, then use a JavaScript return 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 Keyboard Maestro Search Action using RegEx. | ||
+ | |||
+ | ===== Examples ===== | ||
+ | |||
+ | **1. Return Data for Multiple Variables** | ||
+ | |||
+ | {{:action:km-browser-javascript-output.png?nolink|}} | ||
+ | |||
+ | ===== Related Forum Topics ===== | ||
+ | |||
+ | * [Execute JavaScript in Custom Prompt Question ](https://forum.keyboardmaestro.com/t/execute-javascript-in-custom-prompt-question/4491) | ||