Skip to content

Conversation

zero-meta
Copy link
Contributor

Related APIs:

  • webView:registerCallback(name, function): Register a Lua function that can be called from JavaScript
  • webView:on(eventName, listener): Listen for events from JavaScript
  • webView:send(eventName, data): Send data to WebView
  • webView:injectJS(script): Inject JavaScript code into WebView

See more detail in this forum topic and this repo.

Related APIs:
- `webView:registerCallback(name, function)`: Register a Lua function that can be called from JavaScript
- `webView:on(eventName, listener)`: Listen for events from JavaScript
- `webView:send(eventName, data)`: Send data to WebView
- `webView:injectJS(script)`: Inject JavaScript code into WebView

How to use:
```lua
local myWebView = native.newWebView(
    display.contentCenterX,
    display.contentCenterY,
    display.actualContentWidth,
    display.actualContentHeight
)
webView:registerCallback("getDeviceInfo", function(data)
    return {
        platform = system.getInfo("platform"),
        version = system.getInfo("architectureInfo"),
        language = system.getPreference("locale", "language"),
        deviceModel = system.getInfo("model")
    }
end)

webView:on("buttonClicked", function(data)
    print("Button clicked in WebView: " .. data.buttonId)
    -- Respond to WebView
    webView:send("buttonResponse", {message = "Received click for button " .. data.buttonId})
end)

local function injectJS()
    webView:injectJS[[
    function updateDeviceInfo() {
        NativeBridge.callNative("getDeviceInfo").then(info => {
            document.getElementById("deviceInfo").innerHTML =
                `Platform: ${info.platform}<br>
                 Version: ${info.version}<br>
                 Language: ${info.language}<br>
                 Model: ${info.deviceModel}`;
        });
    }

    document.getElementById("updateButton").addEventListener('click', function() {
        updateDeviceInfo();
        NativeBridge.sendToLua("buttonClicked", {buttonId: "update"});
    });

    document.getElementById("greetButton").addEventListener('click', function() {
        NativeBridge.sendToLua("buttonClicked", {buttonId: "greet"});
    });

    NativeBridge.on("buttonResponse", function(data) {
        document.getElementById("response").textContent = data.message;
    });
]]
end

local function webListener( event )
    if event.type == "loaded" then
        injectJS()
    end
end
webView:request( "index.html", system.ResourceDirectory )
webView:addEventListener( "urlRequest", webListener )
```

Here's an example HTML file that works with the above Lua and JavaScript code:
```html
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>WebView Example</title>
   <style>
       body { font-family: Arial, sans-serif; padding: 20px; }
       button { margin: 10px 0; }
   </style>
</head>
<body>
   <h1>WebView Communication Example</h1>
   <div id="deviceInfo"></div>
   <button id="updateButton">Update Device Info</button>
   <button id="greetButton">Send Greeting</button>
   <p id="response"></p>
</body>
</html>
```
Now we will call window.onNativeBridgeLoaded on page loaded.
Use this feature in html:
```html
<!DOCTYPE html>
<html>
<head>
   <title>Test</title>
</head>
<body>
   <p>This is a simple HTML page.</p>
</body>
<script>
    window.onNativeBridgeLoaded = function() {
      // NativeBridge.callNative(method, args)
      // NativeBridge.sendToLua(event, data)
      // NativeBridge.on(event, callback, options)
    }
</script>
</html>
```
…BridgeReady.

Now we will call window.onNativeBridgeReady on page loaded.
Use this feature in html:
```html
<!DOCTYPE html>
<html>
<head>
   <title>Test</title>
</head>
<body>
   <p>This is a simple HTML page.</p>
</body>
<script>
    window.onNativeBridgeReady = function() {
      // NativeBridge.callNative(method, args)
      // NativeBridge.sendToLua(event, data)
      // NativeBridge.on(event, callback, options)
    }
</script>
</html>
```
@zero-meta zero-meta requested a review from Shchvova as a code owner December 4, 2024 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant