c# webbroswer控件如何能有效的阻止广告的弹出,大神们就帮我解决一次吧…
解决方案 »
- 挑战你的智商。看似简单却考验功力的题 生成n个随机数 要求其和等于m
- C# 数据类型转换
- 新人报到,请教如何建立自己的控件
- 想了很久,还是没办法?
- 关于设置BidingSource的Position的死活不变问题
- richtextbox 缩进的问题
- 请问socket server端向client发送讯息
- 关于httpwebrequest的pos 新浪微博数据进行登录的问题
- asp的这句话在asp.net中怎么写?有没有更简单的办法???
- winform 下 datagrid 的cell 如何多行显示,输入?
- 利用wpf如何实现选中某一按钮,鼠标形状改变为自定义形状?
- 如何制作vs2010自定义工具
过滤的难题是:一些广告模块并没有明显特征,过滤法则宽松了会有广告滤不掉,紧了则有可能把网页正常内容也给去除掉。需要代码里进行更多分析。下面贴上我以前写的网页广告过滤代码,我的准则是:只过滤漂浮广告、两侧和右下角广告。因为这类广告可能遮盖正常网页内容,而且这类广告也较易区分判断。
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;
}
{
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;
}
{
e.Cancel = true;
Uri uri = new Uri(((WebBrowser)sender).StatusText);
this.webBrowser1.Url = uri;
}阻止弹窗。你也可以改成阻止广告。你打个断点看看里面的uri就可以进行筛选了
网上找了个。
http://topic.csdn.net/u/20070119/16/418c1439-19f2-4978-a1b8-30dd5207ccc6.html 广告应该也是弹出的一种,不知道可以不可以理解为弹出窗口??这样可能要注入代码了