问题
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;
But the debugger somehow bypasses execution of an obstinately DoWork. In what may be a catch?
回答1:
I got tChromium to work under Delphi 7, it should all work the same.
There are the steps I took to read elements from the DOM
First I got a wrapper from this project: https://code.google.com/p/delphichromiumembedded/downloads/detail?name=dcef-r306.7z&can=2&q=
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;
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.
Have fun coding, Delphi rocks!
来源:https://stackoverflow.com/questions/15598776/delphi-xe3-and-tchromium