问题描述
我有一个TWebBrowser组件,我加载了丰富文本编辑器的URL.编辑后,我想检索文本的HTML(及其所有标记).
I hava a TWebBrowser component into I load the URL of an enriched text editor. After editing I would like to retrieve the HTML of the text (with all its markup).
使用浏览器调试器看一下,我可以看到编辑器将文本存储在iframe中:
Taking a look with the browser debugger I can see the editor stores the text in an iframe:
我可以通过以下方式获取iframe:
I can get the iframe with this:
NodeName := 'htmleditor_ifr';
BodyIframe := (WebBrowser1.Document as IHTMLDocument3 ).getElementById(NodeName);
但是我不知道如何检索内部文档.
But I don't know how to retrieve the inner document.
有什么提示吗?
推荐答案
这是我针对Delphi 7的解决方案.
This is my solution for Delphi 7.
我的Delphi版本不包含IHTMLIFrameElement3的实现,但是IDE提供了一种将其添加到项目中的方法:
My Delphi version didn't contain an implementation of IHTMLIFrameElement3 but the IDE offers a way to add it to your project:
通过此对话框,您可以生成一个新单元,其中包含Delphi 7安装中缺少的所有定义:
With this dialog you can generate a new unit that contains all the definitions missing from the Delphi 7 installation:
CLASS_HTMLFrameElement: TGUID = '{3050F314-98B5-11CF-BB82-00AA00BDCE0B}';
IID_IHTMLIFrameElement: TGUID = '{3050F315-98B5-11CF-BB82-00AA00BDCE0B}';
IID_IHTMLIFrameElement2: TGUID = '{3050F4E6-98B5-11CF-BB82-00AA00BDCE0B}';
IID_IHTMLIFrameElement3: TGUID = '{30510433-98B5-11CF-BB82-00AA00BDCE0B}';
DIID_DispHTMLIFrame: TGUID = '{3050F51B-98B5-11CF-BB82-00AA00BDCE0B}';
CLASS_HTMLIFrame: TGUID = '{3050F316-98B5-11CF-BB82-00AA00BDCE0B}';
[...]
// *********************************************************************//
// Interface: IHTMLIFrameElement3
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {30510433-98B5-11CF-BB82-00AA00BDCE0B}
// *********************************************************************//
IHTMLIFrameElement3 = interface(IDispatch)
['{30510433-98B5-11CF-BB82-00AA00BDCE0B}']
function Get_contentDocument: IDispatch; safecall;
procedure Set_src(const p: WideString); safecall;
function Get_src: WideString; safecall;
procedure Set_longDesc(const p: WideString); safecall;
function Get_longDesc: WideString; safecall;
procedure Set_frameBorder(const p: WideString); safecall;
function Get_frameBorder: WideString; safecall;
property contentDocument: IDispatch read Get_contentDocument;
property src: WideString read Get_src write Set_src;
property longDesc: WideString read Get_longDesc write Set_longDesc;
property frameBorder: WideString read Get_frameBorder write Set_frameBorder;
end;
// *********************************************************************//
// DispIntf: IHTMLIFrameElement3Disp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {30510433-98B5-11CF-BB82-00AA00BDCE0B}
// *********************************************************************//
IHTMLIFrameElement3Disp = dispinterface
['{30510433-98B5-11CF-BB82-00AA00BDCE0B}']
property contentDocument: IDispatch readonly dispid -2147413992;
property src: WideString dispid -2147413991;
property longDesc: WideString dispid -2147413990;
property frameBorder: WideString dispid -2147413989;
end;
// *********************************************************************//
// DispIntf: DispHTMLIFrame
// Flags: (4112) Hidden Dispatchable
// GUID: {3050F51B-98B5-11CF-BB82-00AA00BDCE0B}
// *********************************************************************//
DispHTMLIFrame = dispinterface
['{3050F51B-98B5-11CF-BB82-00AA00BDCE0B}']
procedure setAttribute(const strAttributeName: WideString; AttributeValue: OleVariant;
lFlags: Integer); dispid -2147417611;
function getAttribute(const strAttributeName: WideString; lFlags: Integer): OleVariant; dispid -2147417610;
function removeAttribute(const strAttributeName: WideString; lFlags: Integer): WordBool; dispid -2147417609;
property _className: WideString dispid -2147417111;
property id: WideString dispid -2147417110;
property tagName: WideString readonly dispid -2147417108;
// more
有了这个,我遵循了@Olivier的提示:
Having this I followed the tips from @Olivier:
NodeName := 'htmleditor_ifr';
BodyIframe := (WebBrowser1.Document as IHTMLDocument3 ).getElementById(NodeName);
ContentHTML := (((BodyIframe as IHTMLIFrameElement3 ).contentDocument) as IHTMLDocument2 );
Body := ContentHTML.body.innerHTML;
这篇关于从Delphi中提取TWebBrowser中的iframe内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!