Delphi XE3和TChromium [英] Delphi XE3 and TChromium

查看:137
本文介绍了Delphi XE3和TChromium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Delphi XE3 TChromium中的组件完成工作
需要加载页面内容
论坛中找到了工作代码示例:

Make out the work with a component in Delphi XE3 TChromium need to get to the content of the page is loaded forums found examples of working code:

procedure DoWork (const doc: ICefDomDocument);
var
  q: ICefDomNode;
begin
  q: = doc.GetElementById ('q');
  if Assigned (q) then q.SetElementAttribute ('value', 'Hello, world');
end;

procedure actDomExecute;
var
  q: ICefDomNode;
begin
   crm.Browser.MainFrame.VisitDomProc (DoWork);
end;

但是调试器以某种方式绕过了固执的DoWork的执行。

But the debugger somehow bypasses execution of an obstinately DoWork. In what may be a catch?

推荐答案

我让tChromium在Delphi 7下可以正常工作。

I got tChromium to work under Delphi 7, it should all work the same.

我已经采取了一些步骤来读取DOM中的元素

There are the steps I took to read elements from the DOM

首先,我从这个项目中得到了一个包装器: b $ b https:/ /code.google.com/p/delphichromiumembedded/downloads/detail?name=dcef-r306.7z&can=2&q=

First I got a wrapper from this project: https://code.google.com/p/delphichromiumembedded/downloads/detail?name=dcef-r306.7z&can=2&q=

有也是XE2的一个,如果您需要帮助,将不需要花费太多时间将其转换为XE3。

There is also one for XE2, would not take much to convert that to XE3, if you need help with that I will gladly help on request.

然后声明一个开始和结束铬

Then declare a begin and end in the tchromium

procedure TMainForm.crmLoadStart(Sender: TObject;   const browser: ICefBrowser; const frame: ICefFrame); begin   
  if (browser <> nil) and (browser.GetWindowHandle = crm.BrowserHandle) and ((frame = nil) or (frame.IsMain)) then FLoading := True; 
end;

procedure TMainForm.crmLoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer; out Result: Boolean);
begin
  if (browser <> nil) and (browser.GetWindowHandle = crm.BrowserHandle) and ((frame = nil) or (frame.IsMain)) then begin
    FLoading := False;
    // see if loaded
    while(httpStatusCode <> 200) do begin
      Application.ProcessMessages;
      Sleep(50);
    end;
    browser.GetMainFrame.VisitDomProc(DomProc);
  end;
end;

声明一个称为domproc的过程,如下所示:

Declare a procedure called domproc like so:

procedure DomProc(const Doc: ICefDomDocument);
var
  Node: ICefDomNode;
begin
  url := Doc.BaseUrl;
  if(url='www.goodwebsite.com') then // check if it is the right page and not add
    Node := Doc.Body.Document.GetElementById('idofwhatyouarelookingfor');
  Node.SetElementAttribute('value','Hello world :D');
end;

这是我到目前为止发现的最可靠的方法,您需要确保页面质量良好

That was the most reliable way i have found so far, you need to make sure the page is well loaded and that you are getting the dom for the right frame.

希望它对您有所帮助,请确保查看上面下载链接中的示例代码,这对我有帮助

Hope it helps you, make sure to check out the example code in the download link above, that helped me out a lot.

Delphi玩起来很有趣,

Have fun coding, Delphi rocks!

这篇关于Delphi XE3和TChromium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆