Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# HTML Word url webkit
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):
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 10-May-13 8:42am
Nnorss521
Edited 12-May-13 2:31am
v13
Comments
Sergey Alexandrovich Kryukov at 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
Naourass Derouichi at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
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:
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
  Permalink  
Comments
Naourass Derouichi at 10-May-13 15:59pm
   
can you check my update please?
Sergey Alexandrovich Kryukov at 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
Naourass Derouichi at 10-May-13 16:42pm
   
i got it! thank you very much!
Sergey Alexandrovich Kryukov at 10-May-13 16:46pm
   
Great! Hope you will be able to complete it.
—SA
Naourass Derouichi at 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??
Sergey Alexandrovich Kryukov at 10-May-13 16:56pm
   
I need some thinking on further detail of this implementation... probably you need to use one more event or simply invocation of the call to Navigate (like Dispatcher.Invoke or System.Windows.Forms.Control.Invoke)...
—SA
Naourass Derouichi at 10-May-13 17:04pm
   
ok tell me if you catch something interesting about that
Sergey Alexandrovich Kryukov at 10-May-13 17:07pm
   
It just need some time to play around. Please do it, and I'll keep it in mind, too.
—SA
Naourass Derouichi at 10-May-13 17:17pm
   
i'm really blocked! i tried several codes.. should i open a new discussion about this?
Sergey Alexandrovich Kryukov at 10-May-13 17:27pm
   
You are not supposed to. If you update a question, your post will get back to the top of the list again.
I suggest you publish your newest fixed code using "Improve question" (please comment this comment, so I would give a notification)...
And, did you try my variant with Invoke?
Also, you need to use the debugger.
—SA
Naourass Derouichi at 10-May-13 17:31pm
   
i try to delete the new post but red error message "insufficient permission to delete"
Sergey Alexandrovich Kryukov at 10-May-13 19:15pm
   
No problem; it's probably presently disabled, even for your own posts...
—SA

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

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 12 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100