google-chrome - 如何在chrome浏览器中获取IHtmlDocument2?

标签 google-chrome chromium ihtmldocument2

在 Internet Explorer 中,我可以使用 IHtmlDocument2 来获取 html 文档。

function GetCurrentBrowserDOM: WideString;
var
  hr: HRESULT;
  CurrentIE: IWebbrowser2;
  Wnd: HWND;
  WndChild:HWND;
  document: IDispatch;
  RootNode: IHTMLDocument2;
  innerHtml: WideString;
begin
  Result := '';
  Wnd := GetForegroundWindow;
  WndChild := FindWindowEx(Wnd, 0,'Frame Tab', nil);
  WndChild := FindWindowEx(WndChild, 0,'TabWindowClass', nil);
  WndChild := FindWindowEX(WndChild, 0, 'Shell DocObject View', nil);
  WndChild := FindWindowEX(WndChild, 0, 'Internet Explorer_Server', nil);//find Internet
  CoInitialize(nil);
  try
    hr := GetIEFromHWND(WndChild, CurrentIE);
    if hr = S_OK then
    begin
      document := CurrentIE.Document;
      document.QueryInterface(IID_IHTMLDocument2, RootNode);
      innerHtml := RootNode.body.innerHTML;
    end;
  finally
    CoUninitialize;
  end;
end;

function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
type
  TObjectFromLResult = function(LRESULT: LRESULT; const IID: TGUID; wParam: WPARAM; out PObject): HRESULT; stdcall;
var
  hInst: HWND;
  lRes: Cardinal;
  MSG: Integer;
  pDoc: IHTMLDocument2;
  ObjectFromLresult: TObjectFromLresult;
begin

  hInst := LoadLibrary('Oleacc.dll');
  @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
  if @ObjectFromLresult <> nil then begin
    try
      MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
      SendMessageTimeOut(WHandle, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
      Result := ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);
      Result := GetLastError;
      if Result = S_OK then
        (pDoc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2, IE);
    finally
      FreeLibrary(hInst);
    end;
end;

我用spy++寻找chrome框架 handle ,我找到了它们。

  Wnd := GetForegroundWindow;
  WndChild := FindWindowEx(Wnd, 0, 'Chrome_WidgetWin_0', nil);
  WndChild := FindWindowEx(WndChild, 0, 'Chrome_RenderWidgetHostHWND', nil);
  WndChild := FindWindowEx(WndChild, 0, 'CompositorHostWindowClass', nil);

但它无法捕获名为 ObjectFromLresult 的函数中的结果。

ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);

我收到的错误代码是 127。

这是否意味着chrome浏览器不支持这种方式来获取其html文档?

如果答案是肯定的,还有其他方法可以捕获它吗?

非常感谢。

PS:我尝试过使用MSAA树,但也不起作用。(只能获取标题)

最佳答案

这是不可能的,IHtmlDocument2是仅在IE托管对象中支持的接口(interface),Chrome中使用的WebKit引擎不支持它。不过,您可以使用 MSAA 访问元素,但需要先启用辅助功能:http://www.chromium.org/developers/design-documents/accessibility

或者您可以通过远程调试协议(protocol)访问 DOM:https://developers.google.com/chrome-developer-tools/docs/protocol/1.0/index

关于google-chrome - 如何在chrome浏览器中获取IHtmlDocument2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073532/

相关文章:

google-chrome - 更改Chrome中的默认html5视频播放器

javascript - 如何以编程方式在 Chromium 中添加 HTML DOM 断点?

c++ - Berkelium 编译错误

linux - 将 chromium 安装到 Linux 磁盘镜像?

c# - 从 C# Webbrowser 控件 WPF 中的 Javascript 获取返回值

inheritance - COM 接口(interface)版本控制和继承

delphi - IHTMLStyleSheetRulesCollection 样式标签(内联样式)

javascript - 如何从 GreaseMonkey 用户脚本请求二进制文件?

javascript - Vue.js - 未捕获的类型错误 : Cannot read property 'push' of undefined at <anonymous>:1:12

internet-explorer - HTML 5 发布日期