notification = {}
function notification.currentPermission()
-- static properties seem to be inaccessible with js.window translation
return js.window.eval("Notification.permission")
end
function notification.requestPermission()
local current = notification.currentPermission()
if current == "default" then
return js.window.eval("Notification.requestPermission()")
else
return current
end
end
notification.levels = {info = "ℹ️", warn = "⚠️", caution = "❗", urgent = "🔔", tip = "💡"}
-- for options see https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification#parameters
function notification.show(title, options)
if not title then error("notification title required!") end
if options and not options.icon then
-- set options.icon to "#" (any invalid url) to prevent the 'default' icon
options.icon = js.window.location.origin + "/.client/logo-dock.png"
end
if options and options.level and #options.level != 0 then
local e = notification.levels[options.level]
options.icon = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64"> <text y=".9em" font-size="54">'..e..'</text> </svg>'
end
local current = notification.currentPermission()
if current == "granted" then
return js.new(js.window.Notification, title, options)
else if current == "default" then
-- ask for notification permission in first call
if notification.requestPermission() == "granted" then
notification.show(title, options)
end
end end
end
${widgets.button("Show Notification", function() notification.show("Fizz", {body = "Buzz", requireInteraction = true, level = "tip"}) end)}