OnClientBrowserConsoleMessage

From Multi Theft Auto: Wiki
Jump to navigation Jump to search

This event is triggered when a browser outputs a console message, such as a JavaScript error or a message written using console.log().

Parameters

string message, string source, int line, int level
  • message: the console message.
  • source: the URL or source identifier where the message originated.
  • line: the line number where the message originated.
  • level: the severity level of the message:
    • 0: default
    • 1: verbose
    • 2: info
    • 3: warning
    • 4: error
    • 5: fatal
    • 6: disable

Source

The source of this event is the browser element that generated the console message.

[[{{{image}}}|link=|]] Note: The source parameter is a string identifying the message's origin. It is separate from the event's source variable, which refers to the browser element. Use a different parameter name, such as messageSource, to access both.

Example

This example loads a local HTML page and prints its browser console messages to the debug output.

Client-side Lua:

local browser = createBrowser(25, 25, true, false)

local logLevels = {
    [0] = "default",
    [1] = "verbose",
    [2] = "info",
    [3] = "warning",
    [4] = "error",
    [5] = "fatal",
    [6] = "disable"
}

addEventHandler("onClientBrowserConsoleMessage", browser, function(message, messageSource, line, level)
    outputDebugString(("Browser console message: %s (line: %d, source: %s, level: %s)"):format(
        message, line, messageSource, logLevels[level] or tostring(level)
    ))
end)

addEventHandler("onClientBrowserCreated", browser, function()
    loadBrowserURL(browser, "http://mta/local/test.html")
end)

test.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Browser console example</title>
</head>
<body>
    <script>
        console.log("Hello from the browser!");
        console.warn("This is a warning.");
        console.error("This is an error.");
    </script>
</body>
</html>

Include the HTML file in your resource's meta.xml:

<file src="test.html" />

Use /debugscript 3 to view the output.

See also