JamesDragon Posted December 26, 2023 Share Posted December 26, 2023 Hello guys, does anyone have an idea how to send Lua values to JavaScript to display them in the game, or for example, to show a list of players or run a JavaScript function? Link to comment
Addlibs Posted December 26, 2023 Share Posted December 26, 2023 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 IIYAMA Posted December 27, 2023 Moderators Share Posted December 27, 2023 There is also the executeBrowserJavascript function used to inject JavaScript in to the browser (browser with local context only). Link to comment
JamesDragon Posted December 27, 2023 Author Share Posted December 27, 2023 i work on a ui panel using html and i want fetch a lua array and display it to my html ui using js Link to comment
Moderators IIYAMA Posted December 27, 2023 Moderators Share Posted December 27, 2023 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
JamesDragon Posted December 27, 2023 Author Share Posted December 27, 2023 thank you a lot of but i have one question if i want for exemple execute a js function when a lua event happen Link to comment
Moderators IIYAMA Posted December 27, 2023 Moderators Share Posted December 27, 2023 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) 1 Link to comment
JamesDragon Posted December 27, 2023 Author Share Posted December 27, 2023 Can i also send arguments to the function? Link to comment
Moderators IIYAMA Posted December 27, 2023 Moderators Share Posted December 27, 2023 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now