正 文:
利用webbrowser开发软件时,我们会遇到js的alert警告窗口,当webbrowser在UI主线程时,如果弹出了js警告窗口,没有人去点击这个窗口进行确认,那么整个软件就会堵塞在这里不会再往下运行。这个时候,我们可以采用重定义js的alert函数来实现。
那么如何重定义js的函数了,和飘易一起看有两种方法:
方法一:
WebBrowser屏蔽alert是用重定义alert,confirm实现的。比较简单。代码如下
先添加 com 引用 microsoft html
using mshtml;
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
//string s = "function confirm() {return true;} function alert() {}"; //仅对 body 内有效
string s = @"window.alert = null; window.onerror = null; window.confirm = null; window.open = null; window.showModalDialog = null;";
win.execScript(s, "javascript");
}
如果需要把捕获的js警告窗口的信息传递给软件处理,可以这样:
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
string s = @"function confirm() {return true;}";
s += @"function alert(str) { window.external.alertMessage(str); }";
win.execScript(s, "javascript");
webBrowser1.ObjectForScripting = this;
}
alertMessage(str)为自定义函数,形式如下:
public void alertMessage(string s)
{
MessageBox.Show("接收到alert消息:"+s); //换成你自己想要执行的动作
}
方法二:
继承webBrowser 消息然后屏弊ShowMessageusing System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Test
{
public class MyWebBrowser : System.Windows.Forms.WebBrowser
{
#region ExtendedWebBrowserSite
class ExtendedWebBrowserSite : WebBrowser.WebBrowserSite, UnsafeNativeMethods.IDocHostShowUI
{
public ExtendedWebBrowserSite(WebBrowser host)
: base(host)
{
}
void UnsafeNativeMethods.IDocHostShowUI.ShowMessage(ref UnsafeNativeMethods._RemotableHandle hwnd, string lpstrText, string lpstrCaption, uint dwType, string lpstrHelpFile, uint dwHelpContext, out int plResult)
{
plResult = 0;
//TODO:自定义
}
void UnsafeNativeMethods.IDocHostShowUI.ShowHelp(ref UnsafeNativeMethods._RemotableHandle hwnd, string pszHelpFile, uint uCommand, uint dwData, UnsafeNativeMethods.tagPOINT ptMouse, object pDispatchObjectHit)
{
//TODO:自定义
}
}
protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
{
return new ExtendedWebBrowserSite(this);
}
#endregion
}
public class UnsafeNativeMethods
{
#region IDocHostShowUI
[StructLayout(LayoutKind.Explicit, Pack = 4)]
public struct __MIDL_IWinTypes_0009
{
// Fields
[FieldOffset(0)]
public int hInproc;
[FieldOffset(0)]
public int hRemote;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct _RemotableHandle
{
public int fContext;
public __MIDL_IWinTypes_0009 u;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct tagPOINT
{
public int x;
public int y;
}
[ComImport, Guid("C4D244B0-D43E-11CF-893B-00AA00BDCE1A"), InterfaceType((short)1)]
public interface IDocHostShowUI
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void ShowMessage([In, ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.wireHWND")] ref _RemotableHandle hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrText, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrCaption, [In] uint dwType, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrHelpFile, [In] uint dwHelpContext, [ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.LONG_PTR")] out int plResult);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void ShowHelp([In, ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.wireHWND")] ref _RemotableHandle hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string pszHelpFile, [In] uint uCommand, [In] uint dwData, [In] tagPOINT ptMouse, [Out, MarshalAs(UnmanagedType.IDispatch)] object pDispatchObjectHit);
}
#endregion
}
}
参考一:
http://blog.csdn.net/sql2zy/article/details/5647606参考二:
http://blog.csdn.net/sjzlxd/article/details/5418674 至此,我们可以灵活运用以上的两种方法来屏蔽webbrowser里的js alert 、confirm弹出窗口了。