Writing Javascript

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

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.

# Inputting 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:

# Use array, when input contain 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)

# Use string, when input contains 1 item

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

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

# Console.log - Visible in Chromium

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!)

# Returning 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.

# Nested Arrays

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

# Posting 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()

# Using Pupppeteer

You can use Puppeteer's API within javascript steps.

Find out more here.