What is Custom Javascript and how is it helpful?

Using custom javascript in your automation

Custom Javascript Step

To add a custom javascript step to your automation, hit the + icon then custom javascript to add your code.

Notion image
Notion image
 
 
 
 

When executing JavaScript code from a Chromium browser console, you have access to a variety of properties, including:

  • window: the global object that represents the browser window
  • document: the DOM document object for the current page
  • console: an object used for logging messages to the console
  • fetch: a method used for making HTTP requests
  • localStorage and sessionStorage: objects used for storing data on the client side
  • XMLHttpRequest: an object used for making HTTP requests (an older alternative to fetch)

These properties can be used to manipulate the DOM, make network requests, and store data, among other things.

 

Using local/session storage

 

Your automation will automatically store all session data in a chrome profile locally on your computer. If you are to use localStorage or sessionStorage this will persist through automation runs. To avoid this, you should clear localStorage with a custom javascript step by adding the following code

 
localStorage.clear();
sessionStorage.clear();
 
⚠️
All console output will show in the browser console
 
 

Code Examples

 

Simple console log

console.log("Hello, world!")
Notion image
 
 

Return something for usage in another step

const num = 2;
const numFromTrigger = Number("@mynumber");

return num + numFromTrigger;

If this step was ‘Step 2’ we can now use this in a type step for example as @step 2 which will return what this code computed

 

JS Return from API Call

let url = "https://api.github.com";
const getJSON = async (url) => {
    return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', url, true);
        xhr.onload = function () {
            var status = xhr.status;
            if (status == 200) {
                resolve(xhr.response);
            } else {
                reject(status);
            }
        };
        xhr.send();
    });
};
let ret = await getJSON(url);
console.log(ret);
return ret; //adjust what you want to return here if using in another step
 
Did this answer your question?
😞
😐
🤩