Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept when user click on a link in a webbrowser

I'm trying to intercept tapping on a link in a WebBrowser control. My HTML page contains custom links, for some starting with shared:// I'd like to intercept when the user tap on it.

On iPhone I would use the webView:shouldStartLoadWithRequest:navigationType: method, and look at the URL that is selected.

I haven't managed to reproduce a similar behaviour with Silverlight for Windows Phone.

I do something like:

    {
        webBrowser1.Navigating += new EventHandler<NavigatingEventArgs>(webBrowser1_Navigating);
    }

    void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
    {
        string scheme = null;

        try
        {
            scheme = e.Uri.Scheme; // <- this is throwing an exception here
        }
        catch
        {
        }
        if (scheme == null || scheme == "file")
            return;
        // Not going to follow any other link
        e.Cancel = true;
        if (scheme == "shared")
        {

        }

But I guess an exception when reading some properties of the Uri, when it's a standard Uri with a default file:// URL Additionally, the Navigating event isn't even triggered for links starting with shared://

Now that I'm able to capture tapping on a shared:// I do not care much, but at least I'd like to be able to retrieve the URL we're going to navigate to, and cancel the default operation for a particular URL.

Any ideas what's going on? Thanks

Edit: It turned out that the problem is that the Navigating event is only generated for the following links: file://, http:// or mailto:// The scheme attributes of the Uri is only available for the http:// and mailto:// links

so what I did in the end is replace the shared:// link with http://shared/blah ... And I look at the URL... This works for my purpose. I can now have links that have a different action (like opening an extra window) depending on the links in the html.

like image 715
jyavenard Avatar asked Jan 10 '12 09:01

jyavenard


1 Answers

Here is my final code, in case this is useful for someone in the future:

For an about screen, I use an html file displayed in a WebBrowser component. The about page has a "tell your friend about this app" link as well as links to external web site. It also has local subpages.

Local sub-pages are linked to using a file:// link. Those can be navigated within the WebBrowser component. External links are opened externally with Internet Explorer. Tell your friend link is made of a http://shared link, that opens an email with a pre-set subject and body. Unfortunately, no other scheme than the standard ones are usable as they do not trigger a Navigating event

There's also a support link which is a mailto:// link and opens an EmailComposeTask

    void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
    {
        String scheme = null;

        try
        {
            scheme = e.Uri.Scheme;
        }
        catch
        {
        }
        if (scheme == null || scheme == "file")
            return;
        // Not going to follow any other link
        e.Cancel = true;
        if (scheme == "http")
        {
            // Check if it's the "shared" URL
            if (e.Uri.Host == "shared")
            {
                // Start email
                EmailComposeTask emailComposeTask = new EmailComposeTask();
                emailComposeTask.Subject = "Sharing an app with you";
                emailComposeTask.Body = "You may like this app...";
                emailComposeTask.Show();
            }
            else
            {
                // start it in Internet Explorer
                WebBrowserTask webBrowserTask = new WebBrowserTask();
                webBrowserTask.Uri = new Uri(e.Uri.AbsoluteUri);
                webBrowserTask.Show();
            }
        }
        if (scheme == "mailto")
        {
            EmailComposeTask emailComposeTask = new EmailComposeTask();
            emailComposeTask.To = e.Uri.AbsoluteUri;
            emailComposeTask.Show();
        }
    }
like image 57
jyavenard Avatar answered Sep 28 '22 18:09

jyavenard