Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a form with a webKitBrowser. The user should surf on internet, and when he click a *.doc link, the word document must be shown. where can i tell the browser what to do when the user click on a .doc link?

update:
i've done this (using System.Web.dll):
C#
namespace testGoogleDoc
{
    public partial class Form1 : Form
    {

        static bool IsDocumentToRedirect(string url)
        {
            return url.ToLower().EndsWith(".doc") || url.ToLower().EndsWith(".docx");
        } //IsDocumentToRedirect

        public Form1()
        {

            InitializeComponent();
            browser.Navigate("http://www.google.com");  //BrowserHomePage
        }

        private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
                string urlString = e.Url.ToString();

                if (urlString.ToLower().StartsWith("https://www.google.com/url?q=") || urlString.ToLower().StartsWith("http://www.google.com/url?q="))
                {
                    Uri uri = new Uri(urlString);
                    Uri doc = new Uri(HttpUtility.ParseQueryString(uri.Query).Get("q"));
                    urlString = doc.OriginalString.ToLower();
                }
               if (IsDocumentToRedirect(urlString) && !urlString.ToLower().StartsWith("https://docs.google.com/viewer"))
                {
                    e.Cancel = true;
                    browser.Navigate("https://docs.google.com/viewer?url=" +urlString);
                    
                }
            }


        }
    }


is there a best way to extract the right URL in the query part of the other complexed URL?
Posted
Updated 12-May-13 1:31am
v13
Comments
Sergey Alexandrovich Kryukov 10-May-13 13:54pm    
You should not use such Regex, if any. You don't take into account Doc, dOC, docX, etc. You should use just doc|docx, but with case-insensitive Regex.

Why would you re-post almost identical question? You could better do it on the page of your original question. No matter, it's different. But your "doesn't work" is not informative...
—SA
Nnorss 10-May-13 14:03pm    
ok for regex... i'm still searching for the solution for showing a word file on webKit browser, the biggest problem is that when i click on a .doc link nothing happens with webkitbrowser! i edited the "doesn't work" :D
Sergey Alexandrovich Kryukov 10-May-13 14:09pm    
I got it. As I can see, you got to some alternative idea. It should work, only take into account my answer.
—SA

1 solution

I can see at least one problem: indirect recursion (http://en.wikipedia.org/wiki/Recursion[^]).

When you call Navigate, your browser gets to loading the another document, which leads to invocation of your Load event again. Recursion is "infinite". You need to isolate the case of navigation to "docs.google.com/viewer?url=" from the navigation to a Word document. Also, you need to cancel navigation to a Word document.
Do something like
C#
if (myBrowser.Url.ToString.ToLower().StartsWith("http://docs.google.com/viewer?url=")) exit;

or something like that.

Also, fix your Regex as I explained in my comment to the question, please see. Or, simpler, use strong.ToLower and string.EndsWith, like:
C#
string loUrl = myBrowser.Url.ToString.ToLower();
if (liUrl.endsWith(".docx") || liUrl.endsWith(".doc")) //...

Please see: http://msdn.microsoft.com/en-us/library/system.string.aspx[^].

This is not a final solution, but I think very close. Just work a bit in this direction.

By the way, I don't see a need in WebKit. If you work on Windows only, you can do the same thing with one of the WebBrowse classes already available in .NET FCL.

—SA
 
Share this answer
 
Comments
Nnorss 10-May-13 15:59pm    
can you check my update please?
Sergey Alexandrovich Kryukov 10-May-13 16:16pm    
No, this is not correct. Let's say, you have
http://www.myside.org/myDocument.docx

You will try to navigate to it and, in the handler to (always add "http://"):
http://docs.google.com/viewer?url=http://www.myside.org/myDocument.docx

But this document ends on ".docx" anyway! So it will pass again, to:
http://docs.google.com/viewer?url=http://docs.google.com/viewer?url=http://www.myside.org/myDocument.docx

Do you see the problem?

You should check up two things, not one:
1) if the document is doc/docx?
2) if the URL is already with the "http://docs.google.com/viewer"...

Got the idea? I actually already explained it in first place, in my answer.

—SA
Nnorss 10-May-13 16:42pm    
i got it! thank you very much!
Sergey Alexandrovich Kryukov 10-May-13 16:46pm    
Great! Hope you will be able to complete it.
—SA
Nnorss 10-May-13 16:48pm    
I tried to show the clicked link of the browser in a label. Every time the user click on a link, the link is shown in the label, exept when i click on a *.doc, the value of the label dont change and nothing happen! I know It will be more easy with the default browser, i changed to web kit because it was ver laggy and it freeze the whole application when the user is surfing. can you help me with this please??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900