actions:Execute_a_JavaScript_in_Browser
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| actions:Execute_a_JavaScript_in_Browser [2021/03/25 00:10] – Blue Icon JMichaelTX | actions:Execute_a_JavaScript_in_Browser [2024/01/25 03:06] (current) – peternlewis | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | {{: | ||
| ====== Execute JavaScript in Browser Actions ====== | ====== Execute JavaScript in Browser Actions ====== | ||
| - | There are several *Execute a JavaScript in Browser* actions let you execute a JavaScript in a web browser page: | + | There are several *Execute a JavaScript in Browser* actions |
| - | * _Execute a JavaScript in **[[action: | ||
| * _Execute a JavaScript in **Front Browser**_ | * _Execute a JavaScript in **Front Browser**_ | ||
| * The "Front Browser" | * The "Front Browser" | ||
| - | * Google Chrome | + | * Google Chrome |
| - | * Safari | + | * Safari or Safari-like Browsers like Safari Technology Preview |
| - | * Chrome-based Browsers like Brave Browser | + | |
| * _Execute a JavaScript in **Google Chrome**_ | * _Execute a JavaScript in **Google Chrome**_ | ||
| * _Execute a JavaScript in **Safari**_ | * _Execute a JavaScript in **Safari**_ | ||
| - | All of these Actions | + | **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 | ||
| 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. | ||
| - | {{:actions: | + | In order to control Safari or Chrome you will need to [[assistance:Allow_JavaScript_From_Apple_Events|Allow JavaScript from Apple Events]]. |
| + | |||
| + | You can control which browser | ||
| ===== Options for Returning Results ===== | ===== Options for Returning Results ===== | ||
| Line 29: | Line 40: | ||
| * save results to variable. | * save results to variable. | ||
| * save results to the system clipboard or a named clipboard. | * save results to the system clipboard or a named clipboard. | ||
| - | * asynchronously | + | * asynchronously |
| 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. | ||
| + | |||
| + | ===== Modern Syntax ===== | ||
| + | |||
| + | In version 11.0, scripts default to Modern Syntax, which essentially wraps the script in: | ||
| + | |||
| + | ```javascript | ||
| + | (function () { | ||
| + | … | ||
| + | })() | ||
| + | ``` | ||
| + | |||
| + | This helps keep your script from interacting with the web page in unexpected ways. A result of this is that you need to return values to the action using the `return` syntax. So a trivial action would be: | ||
| + | |||
| + | ```javascript | ||
| + | return " | ||
| + | ``` | ||
| + | |||
| + | You can turn Modern Syntax on or off in the popup menu next to the script. | ||
| ===== Using Keyboard Maestro Variables ===== | ===== Using Keyboard Maestro Variables ===== | ||
| - | JavaScript in browsers can only **GET** | + | JavaScript in browsers |
| + | |||
| + | In Modern Syntax, reference | ||
| ```javascript | ```javascript | ||
| - | // Get Keyboard Maestro variable "My KM Data" | + | var v = kmvar.My_KM_Data |
| - | var myData | + | |
| ``` | ``` | ||
| - | | + | |
| + | In the old syntax, reference the variables like this: | ||
| + | |||
| + | ```javascript | ||
| + | var v = document.kmvar.My_KM_Data | ||
| + | ``` | ||
| + | |||
| + | When accessing a variable, if its name has a space in it, replace it with an underscore. | ||
| + | |||
| + | Local and Instance variables are available, but Password variables are not. | ||
| + | |||
| + | By default, all variables are included, but you can select No Variables, or specific variables as desired using the popup menu next to the script. | ||
| + | |||
| + | (!) Note that in the the old syntax, 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 ===== | ||
| | | ||
| Line 52: | Line 96: | ||
| {{: | {{: | ||
| - | ===== Execute | + | Another approach would be to use `JSON.stringify()` to return |
| - | + | ||
| - | For the *Execute a JavaScript in Custom HTML Prompt* action, you can specify a [[action:Custom_HTML_Prompt]] with a matching [[action: | + | |
| - | + | ||
| - | The JavaScript can access variables by using the window.KeyboardMaestro functions described in the [[action: | + | |
| ===== See Also ===== | ===== See Also ===== | ||
| Line 67: | Line 107: | ||
| === Actions === | === Actions === | ||
| + | * [[action: | ||
| * [[: | * [[: | ||
| - | ==== Forum ==== | ||
| - | |||
| - | - [[https:// | ||
actions/Execute_a_JavaScript_in_Browser.1616631023.txt.gz · Last modified: by JMichaelTX
