locked
Webview: getting current URL when navigating

    Question

  • Hello,

    I would like to have the current url of the webview. I already saw this question: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/3452ab3b-1ccf-4ab2-9f74-c2e1a72d01cb/getting-current-url-in-webview-element?forum=winappswithcsharp

    Unfortunately, if the user choose to go on website with javascript redirection, the event is never fired.

    I also notice there is a native exception coming from a C part. The message is: Invalid parameter passed to C runtime function.

    Is there any way to have the complete url at any time?

    Thursday, October 16, 2014 10:15 AM

Answers

  • I finally found a workaround, that almost fixes all my issues.

    First, if a page use javascript to change URL, the only event fired are Frame Event. This means ContentLoading and NavigationEvent are not working.

    Second, if you make a windows 8.1 application only and you choose to let the user navigate every where on the web, you can't use the injection technique. 

    Windows 8.1

    [..] To enable an external web page to fire the ScriptNotify event when calling window.external.notify, you must include the page's URI in the ApplicationContentUriRules section of the app manifest. 

    My workaround is to use the frame events. This is not perfect, and I still have some issues, but it works in many situation.

    I save the host use in the NavigationCompleted event, if a FrameContentLoading event has the same host, it means javascript was use to update the view.

     private string host;
    
     private void View_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
            {
                    host = args.Uri.Host;
            }
    
            private void View_FrameContentLoading(WebView sender, WebViewContentLoadingEventArgs args)
            {
                if (host == args.Uri.Host)
                {
                     // Do something
                }
            }


    • Marked as answer by BatRocchia Wednesday, October 29, 2014 2:29 PM
    • Edited by BatRocchia Wednesday, October 29, 2014 2:29 PM
    Wednesday, October 29, 2014 2:28 PM

All replies

    • Marked as answer by BatRocchia Friday, October 17, 2014 2:57 PM
    • Unmarked as answer by BatRocchia Friday, October 17, 2014 4:46 PM
    Thursday, October 16, 2014 10:18 PM
    Moderator
  • In my case, there are no event fired by the webview.

    Moreover I have a windows 8.1 app, which means the previous solution is obsolete. (and I don't know on which website the user navigate)

    I am testing the webview on youtube.com and the only error I have is:

    Invalid parameter passed to C runtime function.

    Unfortunately there are no C# events or C# exception I can catch to detect the error.

    Friday, October 17, 2014 5:03 PM
  • What do you mean there are no events fired by WebView?  Which event?  You should be able to register for events just fine.  

    As for the exceptions set your debug mode to Mixed so you can view the native exceptions.


    Bret Bentzinger (MSFT) @awehellyeah

    Friday, October 17, 2014 6:28 PM
    Moderator
  • I need to obtain the current page url. The best way is to get the event fired by the webview.

    Let me explain my workflow:

    1. I register on NavigationCompleted event (LoadCompleted and NavigationFailed events are deprecated)
    2. I start the program and go on youtube.com
    -> The LoadCompleted event is fired, I save the current url in a local string
    3. I start searching on youtube a video, then click the "search button"
    -> LoadCompleted event is not fired.
    4. I click on a video
    -> LoadCompleted event is not fired.

    At the end of the workflow, the user navigate through a website but the local string is never updated. On a navigator, the url is changed using javascript.

    I also use the same workflow on my personal website, which uses javascript to change the url (and angularjs). The LoadCompleted event is also not fired.

    (I noticed the "Invalid parameter passed to C runtime function." error is only on youtube.com, and never on my website.)

    So, my question now can be formulate as:

    Is there any way to have access to the current url, when the user is navigating? Even if the url is modify by javascript and there are no real redirection?

    Wednesday, October 22, 2014 5:09 PM
  • This blog article give the same solution as the previous link: javascript injection on a page.

    On windows 8.1, to allow a page to communicate with the application, I need to add the website to the appmanifest. (probably to avoid "bad" injection)

    In my case, the user can be anywhere on the web, so I can't use this type of solution.

    Friday, October 24, 2014 9:55 AM
  • You will have to use the different events in WebView, along with your application requirements and the injection technique to accomplish what you want.

    Have you looked at the ContentLoading event?  That appears to fire when new content is loaded.  However, it wont tackle the situation where the application doesn't load new content but generates it dynamically.


    Bret Bentzinger (MSFT) @awehellyeah

    Friday, October 24, 2014 5:16 PM
    Moderator
  • I finally found a workaround, that almost fixes all my issues.

    First, if a page use javascript to change URL, the only event fired are Frame Event. This means ContentLoading and NavigationEvent are not working.

    Second, if you make a windows 8.1 application only and you choose to let the user navigate every where on the web, you can't use the injection technique. 

    Windows 8.1

    [..] To enable an external web page to fire the ScriptNotify event when calling window.external.notify, you must include the page's URI in the ApplicationContentUriRules section of the app manifest. 

    My workaround is to use the frame events. This is not perfect, and I still have some issues, but it works in many situation.

    I save the host use in the NavigationCompleted event, if a FrameContentLoading event has the same host, it means javascript was use to update the view.

     private string host;
    
     private void View_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
            {
                    host = args.Uri.Host;
            }
    
            private void View_FrameContentLoading(WebView sender, WebViewContentLoadingEventArgs args)
            {
                if (host == args.Uri.Host)
                {
                     // Do something
                }
            }


    • Marked as answer by BatRocchia Wednesday, October 29, 2014 2:29 PM
    • Edited by BatRocchia Wednesday, October 29, 2014 2:29 PM
    Wednesday, October 29, 2014 2:28 PM