This shows you the differences between two versions of the page.
Next revision | Previous revision Last revision Both sides next revision | ||
actions:Execute_a_JavaScript_in_Browser [2017/08/31 02:02] peternlewis created |
actions:Execute_a_JavaScript_in_Browser [2023/09/12 04:50] peternlewis |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | The *Execute a JavaScript in Browser* actions let you execute a JavaScript in the active web browser page. | + | ====== Execute JavaScript in Browser Actions ====== |
- | Like other [[Browser_Actions|browser actions]], these actions work with Safari or Google Chrome (or whichever of the two was last at the front). | + | There are several *Execute a JavaScript in Browser* actions that let you execute a JavaScript in a web browser page: |
- | Alternatively, you can execute JavaScript in a specified [[action:Custom_HTML_Prompt|Custom HTML Prompt]]. | + | * _Execute a JavaScript in **[[action:Custom_HTML_Prompt|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 or Chrome-based Browsers like Brave Browser | ||
+ | * Safari or Safari-like Browsers like Safari Technology Preview | ||
+ | * _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.brave.Browser.beta | ||
+ | * com.vivaldi.Vivaldi | ||
+ | * com.vivaldi.Vivaldi.snapshot | ||
+ | * com.microsoft.edgemac | ||
+ | |||
+ | 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. | 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]]. | + | {{:actions:execute-javascript-browser-action.png?nolink|}} |
- | {{:action:km-execute-javascript-chrome.png?nolink|}} | + | In order to control Safari or Chrome you will need to [[assistance:Allow_JavaScript_From_Apple_Events|Allow JavaScript from Apple Events]]. |
===== Options for Returning Results ===== | ===== Options for Returning Results ===== | ||
Line 27: | Line 47: | ||
===== Using Keyboard Maestro Variables ===== | ===== Using Keyboard Maestro Variables ===== | ||
- | JavaScript 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: | + | 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: |
```javascript | ```javascript | ||
Line 33: | Line 53: | ||
var myData = document.kmvar.My_KM_Data | var myData = document.kmvar.My_KM_Data | ||
``` | ``` | ||
- | | + | |
+ | Local and Instance variables are available, but Password variables are not. | ||
+ | |||
+ | (!) Note that because your variables are passed to the web page, any other scripts running on that web page will have access them, and this could be a privacy issue if the information is misused by the web page. | ||
+ | |||
===== Saving Results to Keyboard Maestro Variables ===== | ===== Saving Results to Keyboard Maestro Variables ===== | ||
| | ||
- | If you need to set a Keyboard Maestro Variable from the 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 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 [[action: Search_using_Regular_Expression|Search using Regular Expression]]. | 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 [[action: Search_using_Regular_Expression|Search using Regular Expression]]. | ||
Line 43: | Line 68: | ||
{{:action:km-browser-javascript-output.png?nolink|}} | {{:action:km-browser-javascript-output.png?nolink|}} | ||
+ | |||
+ | ===== Execute a JavaScript in Custom HTML Prompt ===== | ||
+ | |||
+ | For the *Execute a JavaScript in Custom HTML Prompt* action, you can specify a [[action:Custom_HTML_Prompt]] with a matching [[action:Custom_HTML_Prompt#HTML_Window_Design|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 [[action:Custom_HTML_Prompt|Custom HTML Prompt]] action. | ||
===== See Also ===== | ===== See Also ===== | ||
Line 58: | Line 89: | ||
- [[https://forum.keyboardmaestro.com/t/execute-javascript-in-custom-prompt-question/4491|Execute JavaScript in Custom Prompt Question]] | - [[https://forum.keyboardmaestro.com/t/execute-javascript-in-custom-prompt-question/4491|Execute JavaScript in Custom Prompt Question]] | ||
- |