Lua

Luau is a typed scripting language derived from Lua. Instead of JavaScript, we use Luau to power Web X.

Note: We say "Luau" as it is what we use in our backend, but you do not need to install it. You can write regular Lua and your code will work fine.

SET and GET.

The Luau API is as simple as learning two things: you can SET and you can GET. You can GET to access elements from your page's HTML++, and you can SET to modify them.

You come from JavaScript?

GET would be equivalent to your document.* query selectors and getElementById / getElementbyClassName, while SET would be equivalent to give a value to props like "href", "opacity", and so on.

GET

In fact, the function to get an element is just called "get".

script.lua
local my_item = get("my_item")
javascript.js
// too long
document.querySelector("h1")
// even longer!?
document.getElementByClassName("my_item")

Note that we use the same function to get items by their tag name or by their class.

Remember the end of the HTML++ page, where we told you to avoid setting a class name with the same name as a tag? It's because of this, get("a") will get an anchor, so you should not give an item a class name of class="a".

What about "querySelectorAll()?

Just add "true" to the get function to get all the elements of the same class / tag name.

script.lua
-- Will give a single anchor
local all_hyperlinks = get("a");
-- Will give all anchors (or hyperlinks, them the way you prefer)
local all_hyperlinks = get("a", true);

When you don't pass true, if you select a tag or a class name that is repeated, the first one in the HTML++ will be taken.

SET

Now, to interact with the element, you can use get_{prop} and set_{prop} functions. Here's an example:

script.lua
local myanchor = get("a")

-- This will GET the href property
local myurl = myanchor.get_href()
print(myurl)

-- This will SET the href property to a new value
myanchor.set_href("buss://dingle.it")

It's that easy! Scroll to the bottom for a list of available SET and GET directives.

Events

The Luau API has also support for detecting events. Similar to JS's onclick. To use them, create a function that's called on an event, just like you would do in JS. Example:

script.lua
get("button").on_click(function()
    print("The button got clicked!!!")
end)

get("input").on_input(function(content)
    print(content)
end)

on_input is only supported by <input> and <textarea>

Output from the print() function will be logged by Napture Logs. As we told you before, you can open them with CONTROL + SHIFT + P

HTTP Fetching

If you need to make an HTTP request to interact with an API, you can use the fetch function. It should look like this:

script.lua
local test = true;

local res = fetch({
    url = "https://api.buss.lol/",
    method = "GET",
    headers = { ["Content-Type"] = "application/json" },
    body = '{ "test": ' .. test .. '}'
})

Variable names are self explanatory, you give the function the URL you want to fetch, the HTTP method you want to use, which can be "GET", "POST", "DELETE", etc..., the headers of the HTTP request, and the body, which would be the content itself of your request.

Full lists

All the get_{x}, set_{x} and on_{x} available functions.

On every function, whenever x is expected to be a string or a number, it can always be passed both as a string/number directly or as a Lua variable.

GET

GETTING FUNCTIONS

Example usage of GET and GETTING FUNCTIONS

script.lua
-- GET
local test = get("myclass")

-- GETTING FUNCTIONS
local opacity = test.get_opacity()
-- can also be called this way
local content = get("h1").get_contents()

-- GET every p tag
local all_paragraphs = get("p", true)

SET

Example usage of SET

script.lua
-- first, we get
local test = get("myclass")

-- now, we set
test.set_opacity(0.75)
test.set_contents("This text will be set as the content of the element")

-- example: get an anchor and set it's href to the Dingle search engine and its text content to "Search with Dingle"
get("a").set_href("buss://dingle.it")
get("a").set_contents("Search with Dingle")

EVENTS

Event functions do not have a return. As showed before, put them inside of a function. Every time they get triggered the code of the functions will be executed.

Example usage of EVENTS

script.lua
local test = get("mybutton")

-- now, we do stuff when it gets clicked
test.on_click(function()
    test.set_contents("i was clicked!")
end)

-- a slightly more complex example:

local input = get("input") -- will get an <input> item
local h1 = get("h1")

input.on_submit(function()
    h1.set_contents("your input was: " + input.get_contents())
)

OTHER FUNCTIONS

About fetch

This is what x (the content of your request) should look like:

fetch.lua
local response = fetch({
    url = "https://api.buss.lol/",
    method = "GET", -- HTTP REQUEST METHOD
    headers = { ["Content-Type"] = "application/json" },
    body = '{ "test": ' .. test .. '}' -- REQUEST BODY
})

Basically url, method, headers, and body. Remember that fetch will return whatever the HTTP request itself returns (the HTTP response, basically).

And that's it! You're ready to write fully functional WebX code! However, we're not done yet. Your beautiful code must be published to the WebX somehow, right? Let's find out about that.

Last updated