title: Interecepting navigation in webview Windows Runtime application
body: <p>I have this code for NDA reasons i will replace all sensitive information with a word domain.     </p>
  
  <p>WebViewPage.xaml</p>
  
  <pre><code>&lt;Page
     x:Class="Domain.Views.WebViewPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="Domain.Views"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"&gt;
  
      &lt;Grid&gt;
      &lt;WebView Name="webView" /&gt;
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>WebViewPage.cs</p>
  
  <pre><code>protected override void OnNavigatedTo(NavigationEventArgs e)
  {
     string url = "http://domain.com/music";
     webView.Navigate(new Uri(url, UriKind.Absolute));
  
     webView.NavigationStarting += webView_NavigationStarting;
     webView.NavigationCompleted += webView_NavigationCompleted;
  }
  
  void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
  {
  }
  
  void webView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
  {   
  }
  </code></pre>
  
  <p>When i open the page the url is loaded. i get the page i wanted.
  Than i have to download music from that page when i click that i get the folowing .</p>
  
  <p><a href="http://imgur.com/PKx6syr" rel="nofollow">http://imgur.com/PKx6syr</a></p>
  
  <p>I belive this is because of the javascript</p>
  
  <pre><code> var downloadTrack = function () {
                  Tracking.recordTrackDownload(track, $scope.isPromoTrack(track));
                  top.location.href = 'domain://musicSelected' + '?track=' + encodeURIComponent(angular.toJson(track));
                  Audio.stop();
              };
  </code></pre>
  
  <p>the link is not a valid link and thats why it shows that you need an app for opening it.</p>
  
  <p>I need information from that link is there a way to read that link.</p>
  
  <p>in android i would do it like this</p>
  
  <pre><code> webView.setWebViewClient(new WebViewClient() {
  
          @Override
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
              progressLoading.setVisibility(View.VISIBLE);
          }
  
          @Override
          public void onPageFinished(WebView view, String url) {
              progressLoading.setVisibility(View.GONE);
          }
  
          @Override
          public void onReceivedError(WebView view,
                                      int errorCode, String description, String failingUrl) {
              progressLoading.setVisibility(View.GONE);
              reportError(errorCode);
          }
  
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String loadingUrl) {
              if (loadingUrl.startsWith("domain://")) {
                  return handleDomainUrl(loadingUrl);
              }
              return false;
          }
  
      });
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I have found this example on MSDN. But this script uses window.external.notify('some string'); in javascript and in our script we have  top.location.href.</p>
  
  <pre><code>public sealed partial class Scenario6 : Page
  {
      // A pointer back to the main page.  This is needed if you want to call methods in MainPage such
      // as NotifyUser()
      MainPage rootPage = MainPage.Current;
  
      public Scenario6()
      {
          this.InitializeComponent();
          string src = "ms-appx-web:///html/scriptNotify_example.html";
          webViewLabel.Text = string.Format("Webview: {0}", src);
          webView6.Navigate(new Uri(src));
          webView6.ScriptNotify += webView6_ScriptNotify;
      }
  
      /// &lt;summary&gt;
      /// Invoked when this page is about to be displayed in a Frame.
      /// &lt;/summary&gt;
      /// &lt;param name="e"&gt;Event data that describes how this page was reached.  The Parameter
      /// property is typically used to configure the page.&lt;/param&gt;
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
      }
  
  
      void webView6_ScriptNotify(object sender, NotifyEventArgs e)
      {
          // Be sure to verify the source of the message when performing actions with the data.
          // As webview can be navigated, you need to check that the message is coming from a page/code
          // that you trust.
          Color c = Colors.Red;
  
          if (e.CallingUri.Scheme =="ms-appx-web")
          {
              if (e.Value.ToLower() == "blue") c = Colors.Blue;
              else if (e.Value.ToLower() == "green") c = Colors.Green;
          }
          appendLog(string.Format("Response from script at '{0}': '{1}'", e.CallingUri, e.Value), c);
      }
  
      /// &lt;summary&gt;
      /// Helper to create log entries
      /// &lt;/summary&gt;
      /// &lt;param name="logEntry"&gt;&lt;/param&gt;
      void appendLog(string logEntry, Color c)
      {
          Run r = new Run();
          r.Text = logEntry;
          Paragraph p = new Paragraph();
          p.Foreground = new SolidColorBrush(c);
          p.Inlines.Add(r);
          logResults.Blocks.Add(p);
      }
  
  }
  </code></pre>
  
  <p>Thank you in advance.</p>