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 [2023/04/03 03:59] – [Using Keyboard Maestro Variables] peternlewis | actions:Execute_a_JavaScript_in_Browser [2024/01/25 03:06] (current) – peternlewis | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| There are several *Execute a JavaScript in Browser* actions that let you execute a JavaScript in a web browser page: | There are several *Execute a JavaScript in Browser* actions that let you execute a JavaScript in a web browser page: | ||
| - | * _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**_ | ||
| Line 19: | Line 17: | ||
| * com.google.Chrome.canary | * com.google.Chrome.canary | ||
| * com.brave.Browser | * com.brave.Browser | ||
| + | * com.brave.Browser.beta | ||
| * com.vivaldi.Vivaldi | * com.vivaldi.Vivaldi | ||
| * com.vivaldi.Vivaldi.snapshot | * com.vivaldi.Vivaldi.snapshot | ||
| + | * com.microsoft.edgemac | ||
| - | All of these Actions | + | 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. | ||
| - | |||
| - | {{: | ||
| In order to control Safari or Chrome you will need to [[assistance: | In order to control Safari or Chrome you will need to [[assistance: | ||
| + | |||
| + | You can control which browser is targeted, whether to trim white space at the start and end of the result, and whether to include any errors in the action (gear) ⚙ menu. | ||
| ===== Options for Returning Results ===== | ===== Options for Returning Results ===== | ||
| Line 40: | 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. | 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. | + | 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 68: | 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 83: | Line 107: | ||
| === Actions === | === Actions === | ||
| + | * [[action: | ||
| * [[: | * [[: | ||
| - | ==== Forum ==== | ||
| - | |||
| - | - [[https:// | ||
actions/Execute_a_JavaScript_in_Browser.1680494365.txt.gz · Last modified: by peternlewis
