iframe 和文档 c# 中的 IE Extensionion/Plugin/Addon JavaScript 注入

本文关键字:Plugin Addon JavaScript 注入 Extensionion IE 文档 中的 iframe | 更新日期: 2023-09-27 18:36:25

我在C#中遇到BHO的问题,Javascript没有在iframe中注入。

如何使用 c++/ATL/COM 访问

iframe 和文档 c# 中的 IE Extensionion/Plugin/Addon JavaScript 注入

我创建了一个IE扩展/插件/插件,以便在页面上以及文档的IFrame附件上注入我的自定义JavaScript

所以我在 c# 中创建了 BHO。 在文档中完成事件

        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 //                   
            }
        }
    }

有关更多详细信息,您可以与我联系:vishalroxx7@gmail.com