Using JavaScript in PixieBrix
Last updated
Last updated
You might use JavaScript in PixieBrix to transform data for your custom needs.
To do this, you'll use the Run Javascript Function
brick.
This brick will replace the JQ brick for data transformation.
To write a function, first add the Run Javascript Function
brick to your mod.
Inside the Brick Configuration Panel, you can define a function, and pass arguments.
Currently, the JavaScript brick only supports the native web API methods in Chrome. To check if a method will work, reference Mozilla's Web APIs docs.
Most functions will require arguments to process data.
Specify the arguments in the Arguments field below the function. Click Add Property to create an item. You can reference variables and outputs from other bricks, arrays of data, strings, other objects, numbers, or boolean values.
You can reference defined arguments to the function in multiple ways (choose one):
Use args
, like function(args)
and set a const below the function like this:
const { arg1, arg2 } = args
;
If you'd like, you can also reference them directly via the params, like function({arg1, arg2})
or you can avoid setting a const
and reference arguments as args.arg1
and args.arg2
If you've been using JQ in existing mods, you'll find ChatGPT is helpful in suggesting a JavaScript function for your JQ filter.
JQ filters are used to transform input JSON data based on the user-defined script. JQ users create a filter to be performed on data. Whereas with JavaScript, you'll create a function and pass it arguments of data. If you're migrating from JQ bricks to JavaScript bricks, your data object will become your argument object, and you'll replace your filter with a function.
Here's an example of how you could use ChatGPT to help you create a function from a JQ filter.
Original JQ filter
Prompt
You can use a prompt like this:
Output from ChatGPT
You might have to tweak it slightly, but ChatGPT is pretty good at giving you the JavaScript version.
Activate this mod and use the Quick Bar to post a JQ filter and the sidebar will return its JavaScript equivalent!
If you're experiencing any issues with your function, try running the function in a JavaScript playground to confirm there are no issues with the function.
You can nest functions within the function, but you cannot have multiple top level functions in the same brick.
For example, this works: ✅
This does not: 🛑
Head to Logs
tab, just above Brick Actions to see the specific error. The Page Editor don't expose this in the brick itself, but you'll be able to see details expanding the row with the error.