Page 1 of 1

Get html source by frame name

Posted: Thu Sep 21, 2017 4:39 pm
by marcoscdoni
Could you please kindly assist with a sample code if possible on how to get html source code for a specific frame?

Re: Get html source by frame name

Posted: Thu Sep 21, 2017 4:56 pm
by salvadordf
Hi,

Take a look at the JSExtension demo and modify the javascript code inside the TJSExtensionFrm.Chromium1ContextMenuCommand procedure to get the HTML code from the desired frame.

You should modify the code for the commandId MINIBROWSER_CONTEXTMENU_JSVISITDOM

Replace this :

Code: Select all

var testhtml = document.body.innerHTML;
with this, assuming you want the frame[1] :

Code: Select all

var testhtml = window.frames[1].document.body.innerHTML;
Read the comments in that demo to understand how it works.

Re: Get html source by frame name

Posted: Thu Sep 21, 2017 8:44 pm
by marcoscdoni
I was testing and I found an easier way to get the frame source code.

See below:


procedure getFrame(const str: ustring);
begin

//-- Do something with str

end;

procedure TFrmTest.Chromium1LoadEnd(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer);
var CefStringVisitor: ICefStringVisitor;
begin


CefStringVisitor := TCefFastStringVisitor.Create(GetFrame);

browser.GetFrame('body').GetSource(CefStringVisitor);

end;

Re: Get html source by frame name

Posted: Fri Sep 22, 2017 8:12 am
by salvadordf
Thank you! :D

I've uploaded a new version of CEF4Delphi with some additions and modifications based on your code.
  • Modified TChromium.RetreiveHTML to get the frame HTML source. Leave the aFrameName parameter empty to get the HTML source from the main frame. The results will be received in the TChromium.OnTextResultAvailable event.
  • Added TChromium.FrameCount property
  • Added TChromium.GetFrameNames function

Re: Get html source by frame name

Posted: Sun Sep 24, 2017 8:12 am
by crystalxp
Hi,salvadordf Cool job,But Sometimes,frame name is empty or has same name more than one ...so I add those code to patch source to get more choose, could it added to office source?:


//uCEFChromium

procedure RetrieveHTML(const aFrameName : ustring = '');overload;
procedure RetrieveHTML(const TargetFrame: ICefFrame); overload;

procedure TChromium.RetrieveHTML(const aFrameName : ustring);
var
TempTask: ICefTask;
begin
if Initialized then
begin
TempTask := TCefGetHTMLTask.Create(self, aFrameName);
CefPostTask(TID_UI, TempTask);
end;
end;

procedure TChromium.RetrieveHTML(const TargetFrame: ICefFrame);
var
TempTask: ICefTask;
begin
if Initialized then
begin
TempTask := TCefGetHTMLTask.Create(Self, TargetFrame);
CefPostTask(TID_UI, TempTask);
end;
end;

procedure Internal_GetHTML(const aFrameName : ustring);overload;
procedure Internal_GetHTML(const TargetFrame: ICefFrame); overload;

procedure TChromium.Internal_GetHTML(const aFrameName : ustring);
var
TempFrame : ICefFrame;
begin
if Initialized then
begin
if (length(aFrameName) > 0) then
TempFrame := FBrowser.GetFrame(aFrameName)
else
TempFrame := FBrowser.MainFrame;

if (TempFrame <> nil) then
begin
if (FVisitor = nil) then FVisitor := TCustomCefStringVisitor.Create(self);
TempFrame.GetSource(FVisitor);
end;
end;
end;

procedure TChromium.Internal_GetHTML(const TargetFrame: ICefFrame);
var
TempFrame: ICefFrame;
begin
if Initialized then
begin
if TargetFrame = nil then
TempFrame := FBrowser.MainFrame
else
TempFrame := TargetFrame;

if (TempFrame <> nil) then
begin
if (FVisitor = nil) then FVisitor := TCustomCefStringVisitor.Create(self);
TempFrame.GetSource(FVisitor);
end;
end;
end;

//uCEFTask

TCefGetHTMLTask = class(TCefTaskOwn)
protected
FChromiumBrowser : TObject;
FFrameName : ustring;
FTargetFrame: ICefFrame;
procedure Execute; override;

public
constructor Create(const aChromiumBrowser : TObject; const aFrameName : ustring); reintroduce; overload;
constructor Create(const aChromiumBrowser : TObject; const TargetFrame: ICefFrame); reintroduce; overload;
end;


constructor TCefGetHTMLTask.Create(const aChromiumBrowser : TObject; const aFrameName : ustring);
begin
inherited Create;

FChromiumBrowser := aChromiumBrowser;
FFrameName := aFrameName;
FTargetFrame := nil;
end;

constructor TCefGetHTMLTask.Create(const aChromiumBrowser: TObject;
const TargetFrame: ICefFrame);
begin
inherited Create;

FChromiumBrowser := aChromiumBrowser;
FFrameName:='';
FTargetFrame := TargetFrame;
end;


procedure TCefGetHTMLTask.Execute;
begin
if (FChromiumBrowser <> nil) and (FChromiumBrowser is TChromium) then
begin
if (FTargetFrame = nil) and (Length(FFrameName) > 0) then
TChromium(FChromiumBrowser).Internal_GetHTML(FFrameName)
else
TChromium(FChromiumBrowser).Internal_GetHTML(FTargetFrame);
end;
end;

Re: Get html source by frame name

Posted: Sun Sep 24, 2017 10:52 am
by salvadordf
Hi crystalxp,

Thank you! :D

I've updated CEF4Delphi with your code and a few more things.