-- Shows a small page in a window and talks to it both ways. Messages from the
-- page are plain strings and untrusted, so parse them defensively.
local webview = require("@neyuki/webview")
local ui = require("@neyuki/ui")
if (not webview.isAvailable()) then
print("no web engine available here")
return
end
local view = webview.open({
title = "Neyuki webview",
width = 420,
height = 260,
html = [[
<body style="font-family: sans-serif">
<h2 id="out">Click the button</h2>
<button onclick="window.neyuki.postMessage('ping')">Ping Neyuki</button>
<script>
window.addEventListener('neyuki', e => {
document.getElementById('out').textContent = JSON.parse(e.detail).text
})
</script>
</body>]],
})
local count = 0
ui.run(function(events, dt)
for _, event in events do
if (event.kind == "webview" and event.type == "message" and event.data == "ping") then
count += 1
event.webview:send({ text = "Pinged " .. count .. " time(s)" })
end
end
end)