从 Delphi 提取 TWebBrowser 中的 iframe 内容

Extract iframe content in TWebBrowser from Delphi

我将 TWebBrowser 组件加载到 URL 丰富的文本编辑器中。编辑后我想检索文本的 HTML(及其所有标记)。

使用浏览器调试器查看,我可以看到编辑器将文本存储在 iframe 中:

我可以用这个获取 iframe:

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

但我不知道如何检索内部文档。

有什么建议吗?

这是我对 Delphi 7.

的解决方案

我的 Delphi 版本不包含 IHTMLIFrameElement3 的实现,但 IDE 提供了一种将其添加到项目中的方法:

Menu Component > Import ActiveX Control

使用此对话框,您可以生成一个新单元,其中包含 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

有了这个,我遵循了@Olivier 的提示:

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