Writing Javascript

It's possible to combine no-code with code, with 'write JavaScript' to manipulate the page or data, any way you wish.

# Set up your environment to write JavaScript

For writing JavaScript, we recommend running axiom's desktop application, and developing locally. This will make it easier to use Chrome's JavaScript console.

Install The Desktop App.

When executed within the 'Interact with page's interface' step, the JavaScript will have access to the browser's DOM and can manipulate the page state.

When executed outside of 'Interact with page' the JavaScript will not have access to the browser DOM.

# Input data to 'Write JavaScript'

If you have scraped data in a previous step, this has store data named scrape-data. You can input this data into the javascript step step, and log it with:

# Input an array containing multiple items

Axiom will replace [scrape-data] with a 2d array containing scraped data from the previous step.

let scrapeData = [scrape-data]
console.log(scrapeData)

# Input a single item

Use a string when the input contains a single item

Axiom will replace [scrape-data] with a string containing scraped data from the previous step.

let scrapeData = '[scrape-data]'
console.log(scrapeData)

# Debug and view data

Use console.log, which is visible in your chromium console.

After the console.log command, this data will be visible on Chromium's console. This can be found the Chromium window axiom opens, rather than your local Chrome browser.

Please add a 'Wait' step after Write JavaScript to see it at the Chromium console (otherwise, your automation may end before you can inspect the console!)

# Return data from Write JavaScript

Any data returned from this step creates a data varaible named code-data, which is then accessible by the other no-code steps.

# Format data to use in other steps

The data format used to pass data between steps in Axiom is a two dimensional array of rows and columns.

You can set the contents of the data by returning an array within an array, like so:

return [[2+2]]

All steps output data in this format, including the content of [scrape-data].

Therefore, it's possible to manipulate this data and return it:

let scrapeData = [scrape-data]
scrapeData[0][1] = 'new value' // This will replace a single element within this scraped data.
return scrapeData

# Post to an API with JavaScript

If you need more sophisticated functionality than the 'trigger webhook' step provides, you can use JavaScript to communicate with an API.

The following template can be used to scrape data from a page, post to to an API endpoint, then store the response in code-data.

//This is data you may have scraped in a previous step
const sData = '[scrape-data]'
//This function will send a request to an endpoint
async function fetchAsync () {
   try {
        let response = await fetch('API URL GOES HERE', {
        	method: 'POST', 
    		headers: {
 			'Content-Type': 'application/json',
    		},
    		body: sData,
  		})
        let data = await response.json();
        await console.log(data)
        return data
    }
    catch (error) {
        await console.log(error)
        return error
    }
}
//Format data in axiom's nested array format
let formatData = async function() { 
    let res = await fetchAsync()
    return [[JSON.stringify(res)]]
}
return formatData()

# Use Puppeteer's API

You can use Puppeteer's API within javascript steps.

This functionality can only be used when either "run in app" is turned on, or when the javascript step is outside an interact loop.

Find out more here.

# Use the node filesystem API

You can use node's filesystem API (fs) within write javascript steps.

This functionality can only be used when either "run in app" is turned on, or when the javascript step is outside an interact loop.

Find out more here (opens new window).

Axiom uses promises in its javascript step, while fs uses callbacks. To get fs operations to work well with axiom, you should always wrap operations in promises:

let result = await new Promise(resolve => {
  fs.readFile('/Users/joe/test.txt', 'utf8', (err, data) => {
    if (err) {
      resolve([[err.message]])
    } else {
      resolve([[data]])
    }
  });
})
return result

# Use ChatGPT

You can use our inbuilt chatGPT function to trigger ChatGPT from within javascript steps.

This functionality can only be used when either "run in app" is turned on, or when the javascript step is outside an interact loop.

You will need a valid OpenAI key to use this feature. You can get one here (opens new window).

Here is an example of how to use this function:

const key = '[YOUR_KEY]'
const prompt = 'Write a short introduction to automation software.'

let result = await chatGPT(key, prompt);
return [[result]];