examples/03-TwoWay.ps1
|
# 03 - Two-way: the page asks, PowerShell answers with Send, the page shows the answer. if (-not (Get-Command Show-NativeWebView -ErrorAction Ignore)) { Import-Module NativeWebView -ErrorAction Stop } $Html = @' <!doctype html> <html><head><style> body { font-family: Verdana, 'Segoe UI', Arial, sans-serif; font-size: 13px; color: #454545; padding: 24px; background: #F9F9F9; } button { padding: 8px 18px; background: #095495; color: white; border: 0; border-radius: 4px; font: inherit; } button:hover { background: #154363; } pre { background: white; padding: 12px; border: 1px solid #CCCCCC; border-radius: 4px; color: #4F8035; } </style></head> <body> <button id="ask">What time is it in PowerShell?</button> <button id="done">Done</button> <pre id="out">-</pre> <script> kHost.on(function (message) { if (message.action === "time") { document.getElementById("out").textContent = message.text; } }); document.getElementById("ask").onclick = function () { kHost.send({ action: "time?" }); }; document.getElementById("done").onclick = function () { kHost.send({ action: "done" }); }; </script> </body></html> '@ Show-NativeWebView -Html $Html -Title "Two-way" -Width 460 -Height 300 -OnMessage { param($Message, $Window) $Data = $Message | ConvertFrom-Json switch ($Data.action) { "time?" { $Window.Send("time", @{ text = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") + " on " + $PSVersionTable.OS }) } "done" { $Window.Close() } } } |