firame标签: IHTMLElement -> IHTMLFrameBase2 -> IHTMLWindow2 -> IHTMLDocument2 跨域访问
2024-08-26 07:26:45
获得iframe标签的元素指针
CComPtr<IHTMLElement> spAdIframe = ...
CComQIPtr<IHTMLFrameBase2> spFrameBase2 = spAdIframe;
CComPtr<IHTMLWindow2> spIframeWindow2 = NULL;
hr = spFrameBase2->get_contentWindow(&spIframeWindow2);
由此可得到iframe元素对应的IHTMLWindow2指针,然而如果直接使用IHTMLWindow2::get_document来获取IHTMLDocument2指针的话,经常得到的是E_ACCESSDENIED错误,因为iframe元素经常用于跨域的访问,即iframe内的文档来源src与其parent的src的hostname不同,但可以通过下面的方法绕过webbrowser的安全检查。
// Converts a IHTMLWindow2 object to a IHTMLDocument2. Returns NULL in case of failure.
// It takes into account accessing the DOM across frames loaded from different domains.
CComQIPtr<IHTMLDocument2> HtmlWindowToHtmlDocument(CComQIPtr<IHTMLWindow2> spWindow)
{
ATLASSERT(spWindow != NULL); CComQIPtr<IHTMLDocument2> spDocument;
HRESULT hRes = spWindow->get_document(&spDocument); if ((S_OK == hRes) && (spDocument != NULL))
{
// The html document was properly retrieved.
return spDocument;
} // hRes could be E_ACCESSDENIED that means a security restriction that
// prevents scripting across frames that loads documents from different internet domains.
CComQIPtr<IWebBrowser2> spBrws = HtmlWindowToHtmlWebBrowser(spWindow);
if (spBrws == NULL)
{
return CComQIPtr<IHTMLDocument2>();
} // Get the document object from the IWebBrowser2 object.
CComQIPtr<IDispatch> spDisp;
hRes = spBrws->get_Document(&spDisp);
spDocument = spDisp; return spDocument;
} // Converts a IHTMLWindow2 object to a IWebBrowser2. Returns NULL in case of failure.
CComQIPtr<IWebBrowser2> HtmlWindowToHtmlWebBrowser(CComQIPtr<IHTMLWindow2> spWindow)
{
ATLASSERT(spWindow != NULL); CComQIPtr<IServiceProvider> spServiceProvider = spWindow;
if (spServiceProvider == NULL)
{
return CComQIPtr<IWebBrowser2>();
} CComQIPtr<IWebBrowser2> spWebBrws;
HRESULT hRes = spServiceProvider->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&spWebBrws);
if (hRes != S_OK)
{
return CComQIPtr<IWebBrowser2>();
} return spWebBrws;
}
附:
IE(控件/接口)中主要有4个部分, Browser, Document, Frame/IFrame, Element , 其对应接口分别是
Browser - IWebBrowser2
Document - IHTMLDocument2
Frame/IFrame- IHTMLWindow2
Element - IHTMLElement
可以通过下面方法互相获取
browser -> document IWebBrowser2::get_Document
document -> frame IHTMLDocument2::get_parentWindow
frame -> document IHTMLWindow2::get_document
frame -> parent frame IHTMLWindow2::get_parent
frame -> children frames IHTMLWindow2::get_frames
element -> Frame IHTMLElement->QI(IHTMLFrameBase2) -> IHTMLFrameBase2->get_contentWindow -> IHTMLWindow2 ->
HtmlWindowToHtmlWebBrowser -> IWebBrowser2
参考:
http://shenan1984.blog.163.com/blog/static/2530851020109201042263/
最新文章
- NOIP2009最优贸易[spfa变形|tarjan 缩点 DP]
- ";undefined method `root&#39; for nil:NilClass"; error when using ";pod install"; 解决办法
- C语言习题(结构)
- C#中的选择语句
- Java中的抽象类
- 细说Linux下软件包的安装与管理
- poj 2739 Sum of Consecutive Prime Numbers 解题报告
- 在VC中显示和处理图片的方法
- SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言]
- 用golang写的生成文件md5sum,检验文件md5sum
- javascript于&;quot;return obj === void 0&;quot;这样的书面理由和优势
- java的return区别
- 【网络爬虫入门04】彻底掌握BeautifulSoup的CSS选择器
- C# 委托详解(一)
- kubernetes进阶(01)kubernetes的namespace
- 未完成的IT路停在回车键---2014年末总结篇
- 深入Ambari Metrics 机制分析
- LTE学习笔记(一)——背景知识
- python itchat+机器人web api实现个人微信机器人
- 基于jQuery+HTML5加入购物车代码
热门文章
- [Tex学习笔记]发一篇文章的经历
- git branch几个简单操作
- Chap5:32– 34
- python笔记一
- <;meta http-equiv=";refresh"; content=";0; url=";>;是什么意思?
- 关于thinkphp 中的字段自动检查机制
- Libevent库 编译与使用
- 在一般处理文件中访问Session需要添加IRequiresSessionState(转载)
- 登陆mysql时出现unknown variable &#39;character_set_client=UTF8&#39; 的错误
- C#算法之向一个集合中插入随机不重复的100个数