不弹出页面,仍在当前浏览器浏览: ExtendedWebBrowser中的StartNewWindow事件在弹出新页面的时候触发.其扩展的事件数据BrowserExtendedNavigatingEventArgs中已经提供了上下文,新页面的URL,ppDisp等所需的数据.所以我们只需改动类BrowserControl中的方法 void _browser_StartNewWindow(object sender, BrowserExtendedNavigatingEventArgs e) 为: void _browser_StartNewWindow(object sender, BrowserExtendedNavigatingEventArgs e) { // Here we do the pop-up blocker work // 这些弹出窗口过滤的代码如果不需要,就全部删除掉 if (allowPopup) { // Check wheter it's a HTML dialog box. If so, allow the popup but do not open a new tab if (!((e.NavigationContext & UrlContext.HtmlDialog) == UrlContext.HtmlDialog)) { ExtendedWebBrowser ewb = mf.WindowManager.New(false); // The (in)famous application object e.AutomationObject = ewb.Application; } } //在这里使用: e.AutomationObject = _browser.Application;似乎没有作用, //但是他原来的代码e.AutomationObject = ewb.Application;是有用的(见上面注释) //不知道是什么原因,只好暂时采用这个办法 _browser.Navigate(e.Url); e.Cancel = true; } 这里如果使用e.AutomationObject = _browser.Application的话,似乎同他原来的代码e.AutomationObject = ewb.Application唯一的区别就是一个browser是浏览过页面了的,一个browser.是新建的不弹出脚本错误提示框: ExtendedWebBrowser通过响应事件WebBrowser.Document.Window.Error来捕获脚本错误提示框弹出的消息: void _browser_DownloadComplete(object sender, EventArgs e) { // Check wheter the document is available (it should be) if (this.WebBrowser.Document != null) { // Subscribe to the Error event this.WebBrowser.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error); . . . void Window_Error(object sender, HtmlElementErrorEventArgs e) { // We got a script error, record it ScriptErrorManager.Instance.RegisterScriptError(e.Url, e.Description, e.LineNumber); // Let the browser know we handled this error. e.Handled = true; } 但是似乎还是由于前面提到的Web页面存在多个框架嵌套的原因,其能够捕获到的消息比较有限,有很大一部分脚本错误提示消息仍然会捕获不到.这样一个问题困扰了我几天.后来突然发现ExtendedWebBrowser里面有如下的代码: /// <summary> /// This method supports the .NET Framework infrastructure and is not intended to be used directly from your code. /// Called by the control when the underlying ActiveX control is created. /// </summary> /// <param name="nativeActiveXObject"></param> [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")] protected override void AttachInterfaces(object nativeActiveXObject) { this.axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)nativeActiveXObject; base.AttachInterfaces(nativeActiveXObject); } 原来AxWebBrowser在这里!于是简单的设置了axIWebBrowser2.Silent属性: protected override void AttachInterfaces(object nativeActiveXObject) { this.axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)nativeActiveXObject; this.axIWebBrowser2.Silent = true;//不弹脚本错误框 base.AttachInterfaces(nativeActiveXObject); } 不再弹出任何提示框,从上面的内容可以看出,ExtendedWebBrowser截取了AxWebBrowser接口.那么,不难想象,虽然我的"再扩展"只扩展了如上三项功能,其实我们现在可以做我们任何想做的! 截获"浏览器的信息对话框"弹出消息: 基于某些特殊需求的需要,程序需要知道浏览器控件的信息对话框何时弹出了,消息的内容是什么,以及其它相关的信息.
ExtendedWebBrowser中的StartNewWindow事件在弹出新页面的时候触发.其扩展的事件数据BrowserExtendedNavigatingEventArgs中已经提供了上下文,新页面的URL,ppDisp等所需的数据.所以我们只需改动类BrowserControl中的方法
void _browser_StartNewWindow(object sender, BrowserExtendedNavigatingEventArgs e)
为:
void _browser_StartNewWindow(object sender, BrowserExtendedNavigatingEventArgs e)
{
// Here we do the pop-up blocker work // 这些弹出窗口过滤的代码如果不需要,就全部删除掉 if (allowPopup)
{
// Check wheter it's a HTML dialog box. If so, allow the popup but do not open a new tab
if (!((e.NavigationContext & UrlContext.HtmlDialog) == UrlContext.HtmlDialog))
{
ExtendedWebBrowser ewb = mf.WindowManager.New(false);
// The (in)famous application object
e.AutomationObject = ewb.Application;
}
}
//在这里使用: e.AutomationObject = _browser.Application;似乎没有作用,
//但是他原来的代码e.AutomationObject = ewb.Application;是有用的(见上面注释)
//不知道是什么原因,只好暂时采用这个办法
_browser.Navigate(e.Url);
e.Cancel = true;
}
这里如果使用e.AutomationObject = _browser.Application的话,似乎同他原来的代码e.AutomationObject = ewb.Application唯一的区别就是一个browser是浏览过页面了的,一个browser.是新建的不弹出脚本错误提示框:
ExtendedWebBrowser通过响应事件WebBrowser.Document.Window.Error来捕获脚本错误提示框弹出的消息:
void _browser_DownloadComplete(object sender, EventArgs e)
{
// Check wheter the document is available (it should be)
if (this.WebBrowser.Document != null)
{
// Subscribe to the Error event
this.WebBrowser.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
.
.
.
void Window_Error(object sender, HtmlElementErrorEventArgs e)
{
// We got a script error, record it
ScriptErrorManager.Instance.RegisterScriptError(e.Url, e.Description, e.LineNumber);
// Let the browser know we handled this error.
e.Handled = true;
}
但是似乎还是由于前面提到的Web页面存在多个框架嵌套的原因,其能够捕获到的消息比较有限,有很大一部分脚本错误提示消息仍然会捕获不到.这样一个问题困扰了我几天.后来突然发现ExtendedWebBrowser里面有如下的代码:
/// <summary>
/// This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.
/// Called by the control when the underlying ActiveX control is created.
/// </summary>
/// <param name="nativeActiveXObject"></param>
[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
protected override void AttachInterfaces(object nativeActiveXObject)
{
this.axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)nativeActiveXObject;
base.AttachInterfaces(nativeActiveXObject);
}
原来AxWebBrowser在这里!于是简单的设置了axIWebBrowser2.Silent属性:
protected override void AttachInterfaces(object nativeActiveXObject)
{
this.axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)nativeActiveXObject;
this.axIWebBrowser2.Silent = true;//不弹脚本错误框
base.AttachInterfaces(nativeActiveXObject);
}
不再弹出任何提示框,从上面的内容可以看出,ExtendedWebBrowser截取了AxWebBrowser接口.那么,不难想象,虽然我的"再扩展"只扩展了如上三项功能,其实我们现在可以做我们任何想做的! 截获"浏览器的信息对话框"弹出消息: 基于某些特殊需求的需要,程序需要知道浏览器控件的信息对话框何时弹出了,消息的内容是什么,以及其它相关的信息.