Jump to content

Js to Lua and Lua to Js


JamesDragon

Recommended Posts

What do you mean by "to JavaScript"? Where is it running? Are you talking about a js script on a website?

If so, you can send and receive data to and from a Js script using HTTP requests to the relevant MTA server web interface (which uses HTTP Basic authentication over unencrypted HTTP). A JavaScript webserver (such as node.js) can do this too.

Note, if you intend to connect to the HTTP web interface for your server, the best practice is to only do HTTP connections internally on the same network, and block the port for any external IPs - instead, route the external traffic through a gateway reverse proxy like nginx or Apache with HTTPs enabled. Otherwise you risk exposing privileged login credentials.

MTA scripts can likewise send and receive data from JS websites using HTTP requests fetchRemote and callRemote. Data can be serialized using toJSON and receive them using fromJSON. This will not work for userdata types, including all elements. You should send names, identifiers or any other data you may need for processing any such element, as they will not be obtainable through JS,

ref and unref functions exist and can allow you to send numerical references to the elements that can be de-referenced at another point in time, but in general, it is better to assign element IDs (setElementID and getElementID) to unique values and use those to send references to these elements. You can look up (de-reference) the element using getElementByID.

Link to comment
  • Moderators
2 hours ago, JamesDragon said:

i work on a ui panel using html

 

You could try something like this.


-- See https://wiki.multitheftauto.com/wiki/ExecuteBrowserJavascript for the rest of browser the code

function reloadList (browser)
    local list = {
        "a",
        "b"
    }

    local htmlItems = {}
    for index, value in ipairs(list) do
        table.insert(htmlItems, [[<li>]] .. value .. [[</li>]])    
    end

    local js = [[{
        const list = document.createElement("ul")
        list.innerHTML = `]] .. table.concat(htmlItems, "\n") ..  [[`;
        document.body.append(list);
    }]]

    executeBrowserJavascript(browser, js)
end

addEventHandler("onClientBrowserDocumentReady", browser,
function ()
    reloadList (source)
end)

See: https://wiki.multitheftauto.com/wiki/OnClientBrowserNavigate when having different pages

Link to comment
  • Moderators
39 minutes ago, JamesDragon said:

f i want for exemple execute a js function when a lua event happen

 

For example you have an html file with:

<script>
	function onRemotePlayerWasted () {
		/* ... */
	}
</script>

And you inject the following JavaScript to call the function.

addEventHandler("onClientPlayerWasted", root, 
function ()
	executeBrowserJavascript(browser, [[onRemotePlayerWasted();]])
end)

 

  • Like 1
Link to comment
  • Moderators
45 minutes ago, JamesDragon said:

Can i also send arguments to the function?

 

Yes, as long as you do not break the code.

 

 

<script>
	function onRemotePlayerWasted (arg) {
		/* ... */
	}
</script>

 

addEventHandler("onClientPlayerWasted", root, 
function ()
	local arg = 123 -- Or string "\"123\"" | "'123'" | '"123"' | [["123"]]
	--[[ 
		Or table through JSON: local arg = '`' .. toJSON({}) .. '`'
		In JS: const result = JSON.parse(arg)
	]]
	executeBrowserJavascript(browser, [[onRemotePlayerWasted(]] .. arg .. [[);]])
end)

 

*adjusted JSON example, Must be tested through trial and error.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...