2016-02-26 77 views
0

我面临与C#中的BHO问题,JavaScript未注入iframe。iframe和文档c中的IE扩展/插件/插件JavaScript注入#

How to get access of <iframe> body using c++/ATL/COM? 问题是类似的,但在上面使用com。我想用C#。

+0

与IE插件浏览器辅助对象(BHO)一个IFRAME访问主体(至少一些数据)--- -----类似的问题在http://stackoverflow.com/questions/5119581/accessing-body-at-least-some-data-in-a-iframe-with-ie-plugin-browser-helper-ob –

+0

同样的问题要求:http://stackoverflow.com/questions/20965171/access-a-span-inside-iframe-using-mshtml –

回答

0

我创建了一个IE浏览器的扩展/插件/附加组件要与

所以我在C#创建BHO文件注入我的自定义JavaScript页面以及对IFrame的武官上。在文档完整的事件

 private void webBrowser_DocumentComplete(object pDisp, ref object URL) 
    { 
     // this is main docuemnt 
     document = (HTMLDocument)webBrowser.Document; 

     //------------------------------------------------------------------------------------- 

     IHTMLElementCollection elcol = document.getElementsByTagName("iframe"); 
     foreach (IHTMLElement _htmlElement in elcol) 
     { 
      try 
      { 
       //This line is for specific iframe on body 
       //string fUrl = ((mshtml.HTMLIFrameClass)_htmlElement).IHTMLFrameBase_src; 
       //if (fUrl.Contains("/v1/api/login?isIframe=true")) 
       //{ 
       // in this casting HtmlElement HTMLFrameElement 
       HTMLFrameElement frmelement = (HTMLFrameElement)_htmlElement; 

       //from HTMLFrameElement Casting as a WebBrowser2 because it will give us body of i frame and works a document . 
       DispHTMLDocument doc2 = (DispHTMLDocument)((SHDocVw.IWebBrowser2)frmelement).Document; 

       //no Doc2 is documnt without access denied .. do what ever you want to do 

       //Here i am checking wheather document having body or not 
       if (doc2.body != null) 
       { 
        //Here i am checking Already injected or not if not then inject my javascript here 
        if (doc2.getElementById("UniqueDivVishal") == null) 
        { 
         IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)doc2.all.tags("head")).item(null, 0); 
         IHTMLScriptElement scriptObject = (IHTMLScriptElement)doc2.createElement("script"); 
         scriptObject.type = @"text/javascript"; 
         scriptObject.text = Properties.Resources.SearchHelper1; 
         ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject); 

         string div2 = "<div id=\"UniqueDivVishal\"></div>"; 
         doc2.body.insertAdjacentHTML("beforeend", div2); 
        } 

        //execute a function from BHO which is in Iframe 
        doc2.parentWindow.execScript("funcation(){ InitFn(); alert('Hello From Frame')}", "javascript"); 

       } 
       //} 
      } 
      catch (Exception ex) 
      { 
       //Handle Exception here //     
      } 
     } 
    } 

进一步的细节可以联系我:[email protected]