.NET WebBrowser控件 - 覆盖IFrame的脚本?

时间:2014-01-11 09:15:58

标签: c# javascript iframe inject

我想暂停/禁用Windows.Form.WebBrowser Control浏览的任何网站的window.alert和window.onbeforeunload。我在主站点上调用它们时成功覆盖了这些函数,但如果在iframe上调用它们,我仍然无法覆盖此函数。

为了重现这个案例,我制作了2个HTML文件

  1. 测试HTML文件:主站点和远程站点
  2. 主要网站:

    <html>
    <head>
    <script>
        window.onbeforeunload = confirmExit;
          function confirmExit()
          {
            return "Onbeforeunload from main";
          }
        alert("Alert from main")
    </script>
    </head>
    <body>
        <a href="http://google.com">Google</a>
        <iframe src="remote.html">
    </body>
    </html>
    

    远程网站:

    <html>
    <head>
    <script>
        window.onbeforeunload = confirmExit;
          function confirmExit()
          {
            return "Onbeforeunload from remote";
          }
        alert("Alert from remote")
    </script>
    </head>
    <body>
        <a href="http://google.com">Google</a>  
    </body>
    </html>
    

    将这2个文件复制到同一个文件夹中(例如复制到桌面)

    1. 将主站点加载到webbrowser并覆盖window.alert / window.onbeforeunload
    2. 初​​始化:

      webBrowser1.Navigate(@"file://127.0.0.1/C$/Users/ServusKevin/Desktop/main.html");
      webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
      webBrowser1.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);
      

      代码:

      private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
      {
          ExecuteMe();
      }
      
      private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
      {
          ExecuteMe();
      }
      
      private void ExecuteMe()
      {
          DisableAlertIE(webBrowser1.Document as HtmlDocument);
          HtmlDocument document = (webBrowser1.Document as HtmlDocument);
          if (document != null)
          {
              for (int index = 0; index < document.Window.Frames.Count; index++)
              {
                  try
                  {
                      DisableAlertIE((document.Window.Frames[index]).Document);
                  }
                  catch
                  {
                  }
              }
          }
      }
      
      private void DisableAlertIE(HtmlDocument document)
      {
          if (document != null)
          {
              //HTMLDocument document = wbIE.Document as HTMLDocument;
              HtmlElement scriptSuppressed = document.CreateElement("script");
              IHTMLScriptElement scriptDomElement = (IHTMLScriptElement)scriptSuppressed.DomElement;
              scriptDomElement.type = "text/javascript";
              scriptDomElement.text = @"window.alert = function () { }; window.confirm = function () { }; window.prompt = function () { }; window.print = function () { }; window.onbeforeunload = function () {};";
      
              foreach (HtmlElement head in document.GetElementsByTagName("head"))
              {
                  head.AppendChild(scriptSuppressed);
              }
          }
      }
      

      当我让程序运行时,主站点的window.alert和window.onbeforeunload已成功暂停,但仍会调用远程站点的alert和onbeforeunload。

      问题:如何改进我的代码以暂停远程站点的警报和onbeforeunload?

0 个答案:

没有答案
相关问题