Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
Get html source by frame name
-
- Posts: 25
- Joined: Thu Sep 21, 2017 4:33 pm
Get html source by frame name
Could you please kindly assist with a sample code if possible on how to get html source code for a specific frame?
- salvadordf
- Posts: 4016
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Get html source by frame name
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 :
with this, assuming you want the frame[1] :
Read the comments in that demo to understand how it works.
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;
Code: Select all
var testhtml = window.frames[1].document.body.innerHTML;
-
- Posts: 25
- Joined: Thu Sep 21, 2017 4:33 pm
Re: Get html source by frame name
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;
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;
- salvadordf
- Posts: 4016
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Get html source by frame name
Thank you!
I've uploaded a new version of CEF4Delphi with some additions and modifications based on your code.
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
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;
//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;
- salvadordf
- Posts: 4016
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Get html source by frame name
Hi crystalxp,
Thank you!
I've updated CEF4Delphi with your code and a few more things.
Thank you!
I've updated CEF4Delphi with your code and a few more things.