- Print
- DarkLight
- PDF
Use the Run JavaScript action to run JavaScript on the current page. You have access to the same scope and functionality that you would have in the Chromium DevTools JavaScript console.
To create a Run JavaScript action, select Add Action > Run JavaScript
Mozenda-specific functions
Mozenda supports the following Mozenda-specific functions:
M_SetFieldValues(<string:field name>, <string: field value>, <bool: terminate row>)
<string: field name> - The name of the field
<string: field value> - The value of the field
<bool: terminate row> - true if you want to create a new line of data after the function is called. false if you will capture other fields/values in the same line in later captures
This form is used when you are capturing a single field. It is roughly equivalent to a Capture Action which captures a single value.
M_SetFieldValues({<string: field name>: <string: field value>, ... }, <bool: terminate row>)
- {<string: field name>: <string: field value>, ... } - A JavaScript object with key/value pairs. The keys are field names and values are field values.
- <bool: terminate row> - true if you want to create a new line of data after the function is called. false if you will capture other fields/values in the same line in later captures
This form is used to capture multiple fields in a single line. It is roughly equivalent to a Capture Action which captures multiple values.
M_SetFieldValues([<rows>])
- [<rows>] - An array of JavaScript objects with key/value pairs.
This is the preferred method to use when running JavaScript which will generate multiple rows of data (e.g. in a loop). This method also increases the speed at which the data will be written to the database in the Web Console.
M_GetFieldValue(fieldName, deleteDownloadedFile)
The M_GetFieldValue() function lets you return a field's value that is already captured in Mozenda. It is similar to using the "%field%" format, but more reliable.
- fieldName - The name of the field whose value you want to get
- deleteDownloadedFile - True or false. If true, the agent will remove the downloaded file associated with the file field requested. If omitted, defaults to false.
- An object representing the value of the associated field with the following attributes
- value - Returns the text value of the field
- Blob - If the field is downloading a file (the field is “file” type), this will return a blob containing the file that was downloaded
- Md5Hash - Similarly, if the field is downloading a file, this will return the file hash of that downloaded file
M_StopWaiting()
By default, when you use a Run JavaScript action, Mozenda will not wait for the code you've written to finish before it moves on (though it may in practice run so quickly that this doesn’t matter). When you call M_StopWaiting() in your code, Mozenda will wait to encounter the M_StopWaiting() call before it moves on to the next action.
M_StopWithError(errorMessage, errorDetail)
M_StopWithError() lets you throw an Agent error from within a Run JavaScript action. This allows you to stop an Agent that uses JavaScript when you encounter unexpected circumstances on a page, just like a well-crafted Item List or Capture Action should error when a page is not as you expected.
- errorMessage - This will populate the “ErrorMessage” Job field
- errorDetail - This will populate the “ErrorDetail” Job field