zoukankan      html  css  js  c++  java
  • C# WebBrowser 屏蔽alert,confirm的方法

    WebBrowser屏蔽alert是用重定义alert,confirm实现的。比较简单。代码如下
    添加 com 引用 microsoft html object library
    using mshtml;

    private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
    IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
    string s = @"function confirm() {";
    s += @"return true;";
    s += @"}";
    s += @"function alert() {}";
    win.execScript(s, "javascript");
    }

    我的不是这样弄的,虽然代码一样,但是我是在执行具体任务的时候,才会写入这个,然后执行这段代码+我自己的点击代码。

    刚才在网上找了下,还有另一种治本的方法呵,高人到处都是。下面是代码,是继承的webBrowser然后屏弊了ShowMessage


    using 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

    }
    }
    这个是直接复制过来的啊,这编辑器没有代码功能,汗。。。有点乱。但很好的屏弊webBrowser里的alert,confirm了。

    不管怎么样,有二种方法,可以实现webBrowser屏弊alert了。

  • 相关阅读:
    JavaScript的函数(二)
    Python:os.walk()和os.path.walk()用法
    Python:代码单元、代码点介绍
    Python:如何去掉字符串中不需要的字符
    Python:更改字典的key
    Python:如何对字符串进行左、右、居中对齐
    Python:format()方法
    日常开发之缓存技术
    Web系统大规模并发——秒杀与抢购 秒杀系统优化与预防措施
    PHP之位运算符
  • 原文地址:https://www.cnblogs.com/qq4004229/p/2443643.html
Copyright © 2011-2022 走看看