开发者

HowTo: WPF Webbrowser return current url / filter porn

I am using the WPF webbrowser and basically when I load an external url, i want to filter any pages that are loaded to make sure the url doesnt contain a swear or porn type word.

This is easy to do on p开发者_StackOverflow中文版age load, as I check the URL against my List badwords; I have also set up a Load Completed method which gets me the url of clicked words, however this isnt working properly :-

    void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
    {
        mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)this.webBrowser1.Document;

        foreach (IHTMLElement link in doc.links)
        {
            HTMLAnchorElement anchor = link as HTMLAnchorElement;
            if (anchor != null)
            {
                HTMLAnchorEvents_Event handler = anchor as HTMLAnchorEvents_Event;
                if (handler != null)
                {
                    handler.onclick += new HTMLAnchorEvents_onclickEventHandler(delegate()
                    {
                        uxURLText.Text = anchor.href;

                        //if (HelperClass.isNotFile(anchor.href))
                       // {

                        if (basepage.nonSafeWords.WordsContainSwearWord(anchor.href))
                        {
                            System.Windows.MessageBox.Show(basepage.INTERNET_RESTRICTION_NOTICE);

                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Word Ok");
                        }
                        return true;
                    });
                }
            }
        }
    }

I need to basically stop any bad content from loading in the window, either from link, button, or ajax if a bad link is clicked a popup needs to notify us. I also need to show the current url in the address bar

Please help many thanks

:)


Listen to the WebBrowser.Navigating Event (docs) and check the Uri in NavigatingCancelEventArgs. Then set e.Cancel = true if you to not want to allow the navigation.

But your valid Uri check is the more complex problem.


private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
    {
    string currentURl= e.Uri.ToString();
    _addrBox.Text = currentURl;
    }

this was the solution in my case. maybe itll help somebody

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜