Skip to main content

Script Block

note

At ditto we're focussed on making it easy to build apps without needing to write code. We consider the use of the script block to be an exception for our platform and hope it's use to be as limited as possible. If there's a script you think others could make good use of as a block, please do get in touch via our social channels.

Script blocks allow you to run custom JavaScript code in your app. This is useful for adding custom functionality that isn't yet available in the standard blocks.

Script Format

Each script block has a single script file which will be executed on the server when the block is loaded.

The script supports asynchronous operations, and therefore must be completed by calling the resolve() function.

Scripts are executed in a sandboxed environment, so you can't access any functions that have not been explicitly provided by us.

Please do get in contact at hello@dittoai.app if you'd like to request any functions be made available.

If the execution of the script takes longer than 10 seconds, it will be terminated immediately and the page will continue loading blocks.

Inputs

The script input area supports the / command to add page fields into the script. This allows you to use the output of other blocks as inputs to your script.

Outputs

Each script block can output data that can be used by other blocks on the page. First add some output fields to your script block by clicking the 'Add Output' button. You can then set the value of these fields in your script by using the output object which is made available to every script block that contains outputs.

fetch function

The fetch function allows you to make HTTP requests from your script. It is a wrapper around the standard fetch function available in the browser, and has the same API. Use this to retrieve data from external APIs or other web resources which can then be saved as outputs in our script block and used throughout your app.

Example

fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(function(response) { return response.json() })
.then(function(json) {
output.title.value = json.title;
output.completed.value = json.completed;
resolve();
});

webhook function

The webhook function provides a simple interface to send json data to a webhook you have set up using a service such as zapier or make.

Example

const jsonData = {
title: 'foo',
completed: true
};
webhook('https://www.site.com/webhooks/yourid', jsonData);