iframe和文档c#中的IE Extention / Plugin / Addon javascript注入

前端之家收集整理的这篇文章主要介绍了iframe和文档c#中的IE Extention / Plugin / Addon javascript注入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在C#中遇到BHO问题,Javascript没有在iframe中注入.

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

解决方法

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

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

  1. private void webBrowser_DocumentComplete(object pDisp,ref object URL)
  2. {
  3. // this is main docuemnt
  4. document = (HTMLDocument)webBrowser.Document;
  5.  
  6. //-------------------------------------------------------------------------------------
  7.  
  8. IHTMLElementCollection elcol = document.getElementsByTagName("iframe");
  9. foreach (IHTMLElement _htmlElement in elcol)
  10. {
  11. try
  12. {
  13. //This line is for specific iframe on body
  14. //string fUrl = ((mshtml.HTMLIFrameClass)_htmlElement).IHTMLFrameBase_src;
  15. //if (fUrl.Contains("/v1/api/login?isIframe=true"))
  16. //{
  17. // in this casting HtmlElement HTMLFrameElement
  18. HTMLFrameElement frmelement = (HTMLFrameElement)_htmlElement;
  19.  
  20. //from HTMLFrameElement Casting as a WebBrowser2 because it will give us body of i frame and works a document .
  21. DispHTMLDocument doc2 = (DispHTMLDocument)((SHDocVw.IWebBrowser2)frmelement).Document;
  22.  
  23. //no Doc2 is documnt without access denied .. do what ever you want to do
  24.  
  25. //Here i am checking wheather document having body or not
  26. if (doc2.body != null)
  27. {
  28. //Here i am checking Already injected or not if not then inject my javascript here
  29. if (doc2.getElementById("UniqueDivVishal") == null)
  30. {
  31. IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)doc2.all.tags("head")).item(null,0);
  32. IHTMLScriptElement scriptObject = (IHTMLScriptElement)doc2.createElement("script");
  33. scriptObject.type = @"text/javascript";
  34. scriptObject.text = Properties.Resources.SearchHelper1;
  35. ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);
  36.  
  37. string div2 = "<div id=\"UniqueDivVishal\"></div>";
  38. doc2.body.insertAdjacentHTML("beforeend",div2);
  39. }
  40.  
  41. //execute a function from BHO which is in Iframe
  42. doc2.parentWindow.execScript("funcation(){ InitFn(); alert('Hello From Frame')}","javascript");
  43.  
  44. }
  45. //}
  46. }
  47. catch (Exception ex)
  48. {
  49. //Handle Exception here //
  50. }
  51. }
  52. }

有关详细信息,请与我联系:vishalroxx7@gmail.com

猜你在找的HTML相关文章