简体   繁体   English

从Delphi中提取TWebBrowser中iframe的内容

[英]Extract iframe content in TWebBrowser from Delphi

I hava a TWebBrowser component into I load the URL of an enriched text editor.我有一个 TWebBrowser 组件,我加载了一个丰富的文本编辑器的 URL。 After editing I would like to retrieve the HTML of the text (with all its markup).编辑后我想检索文本的 HTML(及其所有标记)。

Taking a look with the browser debugger I can see the editor stores the text in an iframe:使用浏览器调试器查看,我可以看到编辑器将文本存储在 iframe 中:

HTML 概述

I can get the iframe with this:我可以通过以下方式获得 iframe:

  NodeName := 'htmleditor_ifr';
  BodyIframe := (WebBrowser1.Document as IHTMLDocument3 ).getElementById(NodeName);

But I don't know how to retrieve the inner document.但我不知道如何检索内部文档。

Any tips?有小费吗?

This is my solution for Delphi 7.这是我对 Delphi 7 的解决方案。

My Delphi version didn't contain an implementation of IHTMLIFrameElement3 but the IDE offers a way to add it to your project:我的 Delphi 版本不包含 IHTMLIFrameElement3 的实现,但 IDE 提供了一种将其添加到项目中的方法:

Menu Component > Import ActiveX Control菜单组件 > 导入 ActiveX 控件

导入 ActiveX 控件对话框

With this dialog you can generate a new unit that contains all the definitions missing from the Delphi 7 installation:使用此对话框,您可以生成一个新单元,其中包含 Delphi 7 安装中缺少的所有定义:

  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

Having this I followed the tips from @Olivier:有了这个,我遵循了@Olivier 的提示:

  NodeName := 'htmleditor_ifr';
  BodyIframe := (WebBrowser1.Document as IHTMLDocument3 ).getElementById(NodeName);
  ContentHTML := (((BodyIframe as IHTMLIFrameElement3 ).contentDocument) as IHTMLDocument2 );
  Body := ContentHTML.body.innerHTML;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM