User Tools

Site Tools


actions:Execute_a_JavaScript_in_Browser

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
actions:Execute_a_JavaScript_in_Browser [2023/09/12 04:50]
peternlewis
actions:Execute_a_JavaScript_in_Browser [2023/09/12 05:23]
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:​Custom_HTML_Prompt|Custom HTML Prompt]]**_ 
 *  _Execute a JavaScript in **Front Browser**_ *  _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:   * The "Front Browser"​ is whichever of the following is currently frontmost, or was the last web browser that was frontmost:
Line 41: 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 ​ — the action ​runs while the macro continues on, no results are returned.+  * asynchronously ​ — the script ​runs while the macro continues on, no results are returned.
  
 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 "​Hello";​
 +```
 +
 +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** ​Keyboard Maestro ​variables They cannot ​be set.  If your Keyboard Maestro ​Variable contains spacesthey must be replaced with underscores in the JavaScript, for example:+JavaScript in browsers ​cannot interact with the outside world, but they can receive the variables from Keyboard Maestro ​in the scriptThe variable values can be read, but writing to them will not alter the corresponding ​Keyboard Maestro ​variables. 
 + 
 +In Modern Syntaxreference ​the variable like this:
  
 ```javascript ```javascript
-// Get Keyboard Maestro variable "My KM Data"​ +var = kmvar.My_KM_Data
-var myData ​document.kmvar.My_KM_Data+
 ``` ```
 +
 +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 pageany other scripts running on that web page will have access themand this could be a privacy issue if the information is misused by the web page.+By default, all variables are includedbut you can select No Variablesor 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 69: Line 96:
 {{:​action:​km-browser-javascript-output.png?​nolink|}} {{:​action:​km-browser-javascript-output.png?​nolink|}}
  
-===== Execute ​JavaScript in Custom HTML Prompt ===== +Another approach would be to use `JSON.stringify()` to return ​JSON structureand then use [[token:JSONValue|%JSONValue%]] token or other JSON facilities ​to extract ​the data.
- +
-For the *Execute a JavaScript in Custom HTML Prompt* actionyou 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 84: Line 107:
 === Actions === === Actions ===
  
 +* [[action:​Execute_a_JavaScript_in_Custom_Prompt|Execute a JavaScript in Custom Prompt]] action
 * [[:​Actions|See all Actions]] * [[:​Actions|See all Actions]]
  
-==== Forum ==== 
- 
-- [[https://​forum.keyboardmaestro.com/​t/​execute-javascript-in-custom-prompt-question/​4491|Execute JavaScript in Custom Prompt Question]] 
actions/Execute_a_JavaScript_in_Browser.txt · Last modified: 2024/01/24 22:06 by peternlewis