OnClientBrowserResourceBlocked: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Please don't create backlinks to your own platform on the MTA wiki)
No edit summary
 
Line 20: Line 20:


==Example==
==Example==
This example asks the user to accept a blocked resource and reloads the browser if accepted.
This example demonstrates how to open a browser window, load a test URL, and handle the onClientBrowserResourceBlocked event. If a resource is blocked, it outputs the details to chat and, if the reason is 0 (not allowed yet), requests the domain and reloads the page if accepted.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local browser = guiCreateBrowser(0, 0, 800, 600, false, false, false)
local screenWidth, screenHeight = guiGetScreenSize( )
local theBrowser = guiGetBrowser(browser)
showCursor(true)


addEventHandler("onClientBrowserCreated", theBrowser, function()
local browser, theBrowser
  loadBrowserURL(source, "https://www.multitheftauto.com/")
local lastBrowser = nil
end)
local testCases = {
    {name = "Google (HTTPS, should work)", url = "https://www.google.com/"},
    {name = "YouTube (HTTPS, should work)", url = "https://www.youtube.com/"},
    {name = "MTA Wiki (HTTPS, should work)", url = "https://wiki.multitheftauto.com/"}
}
 
addCommandHandler("openbrowser", function(_, case)
    if isElement(browser) then destroyElement(browser) end
    local sx, sy = guiGetScreenSize()
    local bw, bh = math.floor(sx * 0.6), math.floor(sy * 0.6)
    local bx, by = math.floor((sx - bw) / 2), math.floor((sy - bh) / 2)
    browser = guiCreateBrowser(bx, by, bw, bh, false, false, false)
    guiSetVisible(browser, true)
    guiBringToFront(browser)
    showCursor(true)
    theBrowser = guiGetBrowser(browser)
 
    local idx = tonumber(case) or 1
    if not testCases[idx] then idx = 1 end
    local test = testCases[idx]
    outputChatBox("[BrowserTest] Loading test case: "..test.name)
 
    addEventHandler("onClientBrowserCreated", theBrowser, function()
        loadBrowserURL(source, test.url)
    end)
 
    addEventHandler("onClientBrowserDocumentReady", theBrowser, function(url)
        outputChatBox("[BrowserTest] Browser loaded: " .. tostring(url))
    end)


local lastBrowser = nil
    addEventHandler("onClientBrowserResourceBlocked", theBrowser, function(url, domain, reason)
addEventHandler("onClientBrowserResourceBlocked", theBrowser, function(url, domain, reason)
        outputChatBox("[BrowserTest] Resource blocked! URL="..tostring(url).." DOMAIN="..tostring(domain).." REASON="..tostring(reason), 255, 0, 0)
  if (reason == 0) then
        if (reason == 0) then
   
            lastBrowser = source
    lastBrowser = source
            requestBrowserDomains({domain}, false, function(accepted, newDomains)
    requestBrowserDomains({domain}, false, function(accepted, newDomains)
                if (accepted == true) then
      if (accepted == true) then
                    outputChatBox("[BrowserTest] Domain accepted, reloading page.")
        reloadBrowserPage(lastBrowser)
                    reloadBrowserPage(lastBrowser)
      end
                else
      lastBrowser = nil
                    outputChatBox("[BrowserTest] Domain NOT accepted.")
                end
                lastBrowser = nil
            end)
        elseif (reason == 1) then
            outputChatBox("[BrowserTest] Domain is blacklisted.")
        elseif (reason == 2) then
            outputChatBox("[BrowserTest] Blocked protocol scheme.")
        end
     end)
     end)
 
  end
end)
end)
</syntaxhighlight>
</syntaxhighlight>
This example sends whitelist requests on demand, which means whenever a blocked domain is detected/queried by the user.
It's also a good alternative to requesting CEF domains on server join, because this is less intrusive and requests will only be sent as soon the player starts interacting with a browser script.
<syntaxhighlight lang="lua">
function resourceBlocked(url,domain,reason)
    if reason == 0 then
        requestBrowserDomains({domain})
    end
end
addEventHandler("onClientBrowserResourceBlocked",browserElement,resourceBlocked)
</syntaxhighlight>


[[pl:onClientBrowserResourceBlocked]]
[[pl:onClientBrowserResourceBlocked]]

Latest revision as of 12:13, 15 July 2025

This event is executed when a resource (images, sounds etc.) has been blocked.

Parameters

string url, string domain, int reason
  • url: the blocked URL.
  • domain: the blocked domain (part of the URL).
  • reason: the reason why the resource was blocked. Possibles values:
    • 0: not allowed yet
    • 1: blacklisted
    • 2: blocked protocol scheme

Source

The browser element.

Example

This example demonstrates how to open a browser window, load a test URL, and handle the onClientBrowserResourceBlocked event. If a resource is blocked, it outputs the details to chat and, if the reason is 0 (not allowed yet), requests the domain and reloads the page if accepted.

local screenWidth, screenHeight = guiGetScreenSize( )

local browser, theBrowser
local lastBrowser = nil
local testCases = {
    {name = "Google (HTTPS, should work)", url = "https://www.google.com/"},
    {name = "YouTube (HTTPS, should work)", url = "https://www.youtube.com/"},
    {name = "MTA Wiki (HTTPS, should work)", url = "https://wiki.multitheftauto.com/"}
}

addCommandHandler("openbrowser", function(_, case)
    if isElement(browser) then destroyElement(browser) end
    local sx, sy = guiGetScreenSize()
    local bw, bh = math.floor(sx * 0.6), math.floor(sy * 0.6)
    local bx, by = math.floor((sx - bw) / 2), math.floor((sy - bh) / 2)
    browser = guiCreateBrowser(bx, by, bw, bh, false, false, false)
    guiSetVisible(browser, true)
    guiBringToFront(browser)
    showCursor(true)
    theBrowser = guiGetBrowser(browser)

    local idx = tonumber(case) or 1
    if not testCases[idx] then idx = 1 end
    local test = testCases[idx]
    outputChatBox("[BrowserTest] Loading test case: "..test.name)

    addEventHandler("onClientBrowserCreated", theBrowser, function()
        loadBrowserURL(source, test.url)
    end)

    addEventHandler("onClientBrowserDocumentReady", theBrowser, function(url)
        outputChatBox("[BrowserTest] Browser loaded: " .. tostring(url))
    end)

    addEventHandler("onClientBrowserResourceBlocked", theBrowser, function(url, domain, reason)
        outputChatBox("[BrowserTest] Resource blocked! URL="..tostring(url).." DOMAIN="..tostring(domain).." REASON="..tostring(reason), 255, 0, 0)
        if (reason == 0) then
            lastBrowser = source
            requestBrowserDomains({domain}, false, function(accepted, newDomains)
                if (accepted == true) then
                    outputChatBox("[BrowserTest] Domain accepted, reloading page.")
                    reloadBrowserPage(lastBrowser)
                else
                    outputChatBox("[BrowserTest] Domain NOT accepted.")
                end
                lastBrowser = nil
            end)
        elseif (reason == 1) then
            outputChatBox("[BrowserTest] Domain is blacklisted.")
        elseif (reason == 2) then
            outputChatBox("[BrowserTest] Blocked protocol scheme.")
        end
    end)
end)

See Also