c# webbroswer控件如何能有效的阻止广告的弹出,大神们就帮我解决一次吧…

解决方案 »

  1.   

    关闭js~~~这个没治的,除非你把那些弹出的js的都给屏蔽掉。这个不是webbroswer的问题,是网站本身的问题,另外还有运营商的问题
      

  2.   

    过滤原理是:等网页加载完毕后,分析网页dom树,根据广告的一些特点,比如Z序等,将广告块从DOM树上移除。
    过滤的难题是:一些广告模块并没有明显特征,过滤法则宽松了会有广告滤不掉,紧了则有可能把网页正常内容也给去除掉。需要代码里进行更多分析。下面贴上我以前写的网页广告过滤代码,我的准则是:只过滤漂浮广告、两侧和右下角广告。因为这类广告可能遮盖正常网页内容,而且这类广告也较易区分判断。
    using mshtml;void extendWebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
         if (!this.extendWebBrowser1.IsBusy)
         {
              FilterFloat(this.extendWebBrowser1);
              FilterFloat2(this.extendWebBrowser1);
         }
    }         private int ContainerCount(WebBrowser obj)
            {
                IHTMLDocument2 HTMLDocument = (IHTMLDocument2)obj.Document.DomDocument;
                IHTMLDOMNode bodyNode = (IHTMLDOMNode)HTMLDocument.body;            int count = 0;            if (bodyNode != null && bodyNode.hasChildNodes())
                {
                    IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)bodyNode.childNodes;
                    int length = allchild.length;                for (int i = 0; i < length; i++)
                    {
                        IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);                    if (IsContainer(child_node.nodeName))
                        {
                            count++;
                        }
                    }
                }            return count;
            }
            
            private void FilterFloat(WebBrowser obj)
            {
                /*
                IHTMLDocument3 HTMLDocument = (IHTMLDocument3)obj.Document.DomDocument;
                IHTMLDOMNode rootDomNode = (IHTMLDOMNode)HTMLDocument.documentElement;
                IHTMLDOMNode bodyNode = null;            if (rootDomNode.hasChildNodes())
                {
                    IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)rootDomNode.childNodes;
                    int length = allchild.length;
                    for (int i = 0; i < length; i++)
                    {
                        IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);
                        if (string.Equals(child_node.nodeName,"body",StringComparison.OrdinalIgnoreCase))
                        {
                            bodyNode = child_node;
                            break;
                        }
                    }
                }*/
     
                try
                {
                    IHTMLDocument2 HTMLDocument = (IHTMLDocument2)obj.Document.DomDocument;
                    IHTMLDOMNode bodyNode = (IHTMLDOMNode)HTMLDocument.body;                ArrayList del = new ArrayList();                if (bodyNode != null && bodyNode.hasChildNodes())
                    {
                        IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)bodyNode.childNodes;
                        int length = allchild.length;
                        for (int i = 0; i < length; i++)
                        {
                            IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);
                            if(this.extendWebBrowser1.IsBusy)
                            {
                                break; 
                            }                        if (IsContainer(child_node.nodeName))
                            {
                                IHTMLDOMNode fix = GetFixedDiv(child_node);                            if (fix != null)
                                {
                                    if (NeedFilter(fix))
                                    {
                                        del.Add(fix);
                                    }
                                }
                            }
                            //System.Windows.Forms.Application.DoEvents();
                        }                    for (int i = 0; i < del.Count; ++i)
                        {
                            IHTMLDOMNode node = (IHTMLDOMNode)del[i];
                            node.removeNode(true);
                            string text = node.nodeName + (node.firstChild == null ? "" : node.firstChild.nodeName);
                            this._Notification((int)APP_Mht.APP_Mht_FilterStyle.Float,text );
                        }
                    }
                }
                catch
                { }
            }        private IHTMLDOMNode GetFixedDiv(IHTMLDOMNode node)
            {
                if (node != null)
                {
                    try
                    {
                        IHTMLStyle style = ((IHTMLElement)node).style;
                        string zindex = style.zIndex.ToString();
                        int zi = Convert.ToInt32(zindex);                    if (IsContainer(node.nodeName))
                        {
                            if (string.Equals("fixed", style.position, StringComparison.OrdinalIgnoreCase) ||
                                zi > 10000)
                            { return node; }
                        }                    if (node.hasChildNodes())
                        {
                            IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)node.childNodes;
                            int length = allchild.length;                        if (length <= 4)
                            {
                                for (int i = 0; i < length; i++)
                                {
                                    IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);
                                    if (IsContainer(child_node.nodeName))
                                    {
                                        IHTMLDOMNode re = GetFixedDiv(child_node);
                                        if (re != null)
                                        { return re; }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    { }
                }
                return null;
            }        private bool IsContainer(string tagName)
            {
                if (string.Equals("div", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("span", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("table", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("ins", tagName, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
                else
                { return false; }
            }        /// <summary>
            /// 如果node子孙节点(不包括iframe内的)包含有除容器标签外的其它标签,返回false。
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            private bool IsContainer(IHTMLDOMNode node)
            {
                if (node != null && node.hasChildNodes())
                {
                    IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)node.childNodes;
                    int length = allchild.length;                if (length > 4)
                    { return false; }                for (int i = 0; i < length; i++)
                    {
                        IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);                    if (!IsContainer2(child_node.nodeName))
                        {
                            return false;
                        }                    if (!string.Equals(child_node.nodeName, "iframe", StringComparison.OrdinalIgnoreCase) &&
                            !string.Equals(child_node.nodeName, "object", StringComparison.OrdinalIgnoreCase)
                            && !IsContainer(child_node))
                        { return false; }
                    }
                }
                return true;
            }        private bool NeedFilter(IHTMLDOMNode node)
            {
                if (node != null && node.hasChildNodes())
                {
                    IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)node.childNodes;
                    int length = allchild.length;                if (length <= 4)
                    {
                        for (int i = 0; i < length; i++)
                        {
                            IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);                        if (string.Equals(child_node.nodeName, "img", StringComparison.OrdinalIgnoreCase) ||
                                string.Equals(child_node.nodeName, "object", StringComparison.OrdinalIgnoreCase) ||
                                string.Equals(child_node.nodeName, "iframe", StringComparison.OrdinalIgnoreCase))
                            {
                                return true;
                            }                        if (NeedFilter(child_node))
                            { return true; }
                        }
                    }
                }
                return false;
            }
      

  3.   

    续:        private void FilterFloat2(WebBrowser obj)
            {
                try
                {
                    ArrayList del = new ArrayList();                Point[] pts = new Point[6];
                    pts[0] = new Point(64, obj.Height / 2);
                    pts[1] = new Point(obj.Width - 64, obj.Height / 2);
                    pts[2] = new Point(obj.Width - 100, obj.Height - 100);                pts[3] = new Point(64, obj.Height - 120);
                    pts[4] = new Point(64, obj.Height - 200);
                    pts[5] = new Point(64, obj.Height / 2 - 64);                foreach (Point pt in pts)
                    {
                        mshtml.IHTMLDocument2 document = (mshtml.IHTMLDocument2)obj.Document.DomDocument;
                        IHTMLElement el = document.elementFromPoint(pt.X, pt.Y);                    if (this.extendWebBrowser1.IsBusy)
                        { break; }                    if (el != null)
                        {
                            IHTMLDOMNode filter = null;
                            IHTMLDOMNode node = NeedFilter2((IHTMLDOMNode)el, out filter);                        if (node != null && IsContainer(node))
                            {
                                IHTMLElement ele = (IHTMLElement)filter;
                                if (ele.offsetWidth + ele.offsetHeight < 700)
                                {
                                    del.Add(node);
                                }
                            }
                            //System.Windows.Forms.Application.DoEvents();
                        }
                    }                for (int i = 0; i < del.Count; ++i)
                    {
                        IHTMLDOMNode node = (IHTMLDOMNode)del[i];
                        node.removeNode(true);
                        string text = node.nodeName + (node.firstChild == null ? "" : node.firstChild.nodeName);
                        this._Notification((int)APP_Mht.APP_Mht_FilterStyle.Float, text);
                    }
                }
                catch (Exception)
                { }
            }        /// <summary>
            /// 判断node父结点中是否有需要过滤的,如有返回需要过滤的节点。
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            private IHTMLDOMNode NeedFilter2(IHTMLDOMNode node)
            {
                ArrayList nodes = new ArrayList();
                nodes.Add(node);
                IHTMLDOMNode parent = node.parentNode;            while (parent != null)
                {
                    nodes.Add(parent);
                    parent = parent.parentNode;
                }            IHTMLDOMNode Node = null;
                for (int i = 0; i < nodes.Count; ++i)
                {
                    IHTMLDOMNode nd = (IHTMLDOMNode)nodes[i];
                    if (nd.parentNode != null &&
                        string.Equals(nd.parentNode.nodeName, "body", StringComparison.OrdinalIgnoreCase))
                    {
                        Node = nd;
                        break;
                    }
                }            if (Node != null && IsContainer(Node.nodeName))
                {
                    if (NeedFilter(Node))
                    { return Node; }
                }            return null;
            }        private bool IsContainer2(string tagName)
            {
                if (string.Equals("div", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("span", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("table", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("img", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("object", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("iframe", tagName, StringComparison.OrdinalIgnoreCase) ||
                    string.Equals("#text", tagName, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
                else
                { return false; }
            }        private IHTMLDOMNode NeedFilter2(IHTMLDOMNode node, out IHTMLDOMNode filter)
            {
                ArrayList nodes = new ArrayList();
                nodes.Add(node);
                IHTMLDOMNode parent = node.parentNode;            filter = null;            while (parent != null)
                {
                    nodes.Add(parent);
                    parent = parent.parentNode;
                }            IHTMLDOMNode Node = null;
                for (int i = 0; i < nodes.Count; ++i)
                {
                    IHTMLDOMNode nd = (IHTMLDOMNode)nodes[i];
                    if (nd.parentNode != null &&
                        string.Equals(nd.parentNode.nodeName, "body", StringComparison.OrdinalIgnoreCase))
                    {
                        Node = nd;
                        break;
                    }
                }            if (Node != null && IsContainer(Node.nodeName))
                {
                    if (NeedFilter(Node, out filter))
                    { return Node; }
                }            return null;
            }        private bool NeedFilter(IHTMLDOMNode node, out IHTMLDOMNode child)
            {
                child = null;            if (node != null && node.hasChildNodes())
                {
                    IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)node.childNodes;
                    int length = allchild.length;                if (length < 4)
                    {
                        for (int i = 0; i < length; i++)
                        {
                            IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);                        if (//string.Equals(child_node.nodeName, "img", StringComparison.OrdinalIgnoreCase) ||
                                string.Equals(child_node.nodeName, "object", StringComparison.OrdinalIgnoreCase) ||
                                string.Equals(child_node.nodeName, "iframe", StringComparison.OrdinalIgnoreCase))
                            {
                                child = node;
                                return true;
                            }                        if (NeedFilter(child_node, out child))
                            { return true; }
                        }
                    }
                }
                return false;
            }
       
      

  4.   

    private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
            {
                e.Cancel = true;       
                Uri uri = new Uri(((WebBrowser)sender).StatusText);
                this.webBrowser1.Url = uri; 
            }阻止弹窗。你也可以改成阻止广告。你打个断点看看里面的uri就可以进行筛选了
      

  5.   

    以前的广告是弹页面出来 容易识别阻止  现在 从流行css+div后 不在弹出窗体了 而是层 ,过滤难度太大 而且没有 完全阻止的可能 
      

  6.   

    判断不是自己要的url就除去.是自己的就不除
      

  7.   

    是不是广告都是要判断的.你可以符合某个条件的url通过.不符合的禁止
      

  8.   

    呵呵,按理说,webbrower只是个载体,里面的东西,它不关心了。看看能不设置想办法设置浏览页的属性,阻止广告弹出~
    网上找了个。
    http://topic.csdn.net/u/20070119/16/418c1439-19f2-4978-a1b8-30dd5207ccc6.html  广告应该也是弹出的一种,不知道可以不可以理解为弹出窗口??这样可能要注入代码了