UWP WebView 如何获取任何网站的警报消息
How to get any web site alert message UWP WebView
我做错了什么?
我用 w3schools.com 进行测试。
webView.Navigate(new Uri("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert"));
Package.appxmanifest 文件
导航完成
private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string result = await sender.InvokeScriptAsync("eval", new string[] { "window.alert = function (AlertMessage) {window.external.notify(AlertMessage)}" });
}
ScriptNotify
private async void WebView_ScriptNotify(object sender, NotifyEventArgs e)
{
MessageDialog dialog = new MessageDialog(e.Value);
await dialog.ShowAsync();
}
这里的问题与网页有关
(https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert) 你正在用来测试。
如果您查看此页面,您会发现 "Try it" 按钮实际上位于名为 "iframeResult" 的 iframe
中。
<iframe frameborder="0" id="iframeResult" name="iframeResult">
<!DOCTYPE html>
<html>
<head></head>
<body contenteditable="false">
<p>Click the button to display an alert box.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("Hello! I am an alert box!");
}
</script>
</body>
</html>
</iframe>
因此您的代码在单击 "Try it" 时将不起作用,因为您正在覆盖 window.alert
,这是父框架中的 alert
方法.为了使您的代码正常工作,您只需将其更改为 iframeResult.window.alert
,如下所示:
private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string result = await sender.InvokeScriptAsync("eval", new string[] { "iframeResult.window.alert = function(AlertMessage) {window.external.notify(AlertMessage)}" });
}
我做错了什么?
我用 w3schools.com 进行测试。
webView.Navigate(new Uri("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert"));
Package.appxmanifest 文件
导航完成
private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string result = await sender.InvokeScriptAsync("eval", new string[] { "window.alert = function (AlertMessage) {window.external.notify(AlertMessage)}" });
}
ScriptNotify
private async void WebView_ScriptNotify(object sender, NotifyEventArgs e)
{
MessageDialog dialog = new MessageDialog(e.Value);
await dialog.ShowAsync();
}
这里的问题与网页有关 (https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert) 你正在用来测试。
如果您查看此页面,您会发现 "Try it" 按钮实际上位于名为 "iframeResult" 的 iframe
中。
<iframe frameborder="0" id="iframeResult" name="iframeResult">
<!DOCTYPE html>
<html>
<head></head>
<body contenteditable="false">
<p>Click the button to display an alert box.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("Hello! I am an alert box!");
}
</script>
</body>
</html>
</iframe>
因此您的代码在单击 "Try it" 时将不起作用,因为您正在覆盖 window.alert
,这是父框架中的 alert
方法.为了使您的代码正常工作,您只需将其更改为 iframeResult.window.alert
,如下所示:
private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string result = await sender.InvokeScriptAsync("eval", new string[] { "iframeResult.window.alert = function(AlertMessage) {window.external.notify(AlertMessage)}" });
}