2017-02-24 22 views
0

何が間違っているのですか?Webサイトのアラートメッセージを取得する方法UWP WebView

私はテストのためにw3schools.comを使用しました。

webView.Navigate(new Uri("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert")); 

Package.appxmanifestファイル

enter image description here

NavigationCompleted

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(); 
} 
+0

あなたが起こることを期待し何をすべきか、そして、それは代わりに何をしているのですか? – Adam

+0

私は警告メッセージ([スクリーンショット]のような)を取得したいです(https://i.stack.imgur.com/IHbud.png) –

答えて

0

ここでの問題は関連していますWebページ (https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert)にテストしています。

このページを調べると、「試してください」ボタンが実際には「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>

だからあなたのコードを使用すると、親フレームでalert方法であるwindow.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)}" }); 
} 
+0

これは、他のサイトのiframeでは動作しません。なぜなら、id 'iframeResult' w3schoolsに固有です。 – jerone

関連する問題