CEF4 Get HTML Source

Post Reply
extasy
Posts: 5
Joined: Mon Jul 05, 2021 6:19 pm
Location: UK
Contact:

CEF4 Get HTML Source

Post by extasy »

Good day. I understand that the topic has been raised more than once and there have been similar answers on many forums. And right away I want to apologize very much for the flood of the same issue that has been repeatedly raised on this forum and many other forums. But the fact is that not a single answer suited me .... most likely the point is in me: (((((
I've rummaged through a bunch of forums with similar questions ... I don't know what to do already.

I don’t understand how to correctly get the html code of the page loaded in chromium сef4. I try it on chromium 91.0 and Delphi 7. First, I had a question about the combination of two components: Chromium1 and CEFWindowParent1. I have not been able to solve it. There are a lot of options on the Internet, you need to initialize it when you open the form, but then after these actions nothing happens ... The page does not load.
After all TChromium.RetrieveHTML and TChromium.OnTextResultAvailable. But, nothing happens either. I don’t know the answer, maybe it doesn’t work at all on Delphi 7. Please, I beg you to help me.

Here's what I get

Code: Select all

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, sMemo, sButton, uCEFChromiumCore, uCEFChromium,
  uCEFWinControl, uCEFWindowParent, uCEFLinkedWinControlBase,
  uCEFChromiumWindow;

type
  TForm1 = class(TForm)
    Chromium1: TChromium;
    Button1: TsButton;
    Memo1: TsMemo;
    CEFWindowParent1: TCEFWindowParent;
    procedure FormCreate(Sender: TObject);
    procedure sButton1Click(Sender: TObject);
    procedure Chromium1TextResultAvailable(Sender: TObject;
      const aText: string);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Chromium1.CreateBrowser(CEFWindowParent1, '');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Chromium1.LoadURL('https://google.com');
Chromium1.RetrieveHTML;
end;

procedure TForm1.Chromium1TextResultAvailable(Sender: TObject;
  const aText: string);
begin
 Memo1.Text :=  aText;
end;
As a result, the site does not load and the source code in Memo1 is not obtained

4 Components: Chromium1, CEFWindowParent1, Memo, Button
There are all libraries with the project, but it still does not work, it does not even show an error.
Delphi 7
Help me please :(((
User avatar
salvadordf
Posts: 3067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CEF4 Get HTML Source

Post by salvadordf »

Hi,

The code you posted only needs a couple of changes :
  • Chromium1.CreateBrowser may fail if it's called too soon. Use the TForm.OnShow event to call Chromium1.CreateBrowser and add a timer to retry if it fails. Most of the demos use this solution. Read this for more information : https://www.briskbard.com/index.php?lang=en&pageid=cef#creation
  • Chromium1.LoadURL is asynchronous and it takes some time before the page is fully loaded. This means that Chromium1.RetrieveHTML may give the wrong results if it's called before the browser finishes loading the page. Use TChromium.OnLoadingStateChange and check "isLoading" to know when the browser has finished loading the page and then call Chromium1.RetrieveHTML.
There are only two demos ready to run in Delphi 7 but you can check the code of the other demos or install Lazarus to run many more demos.

I would recommend you to use one of the demos as a template for your application. Please, read this document too :
https://www.briskbard.com/index.php?lang=en&pageid=cef
extasy
Posts: 5
Joined: Mon Jul 05, 2021 6:19 pm
Location: UK
Contact:

Re: CEF4 Get HTML Source

Post by extasy »

I made adjustments to the code, as you wrote, except for the timer, but the program still does not want to receive the HTML code

Code: Select all


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, sMemo, sButton, uCEFWinControl, uCEFWindowParent,
  uCEFChromiumCore, uCEFChromium, uCEFInterfaces;

type
  TForm1 = class(TForm)
    Chromium1: TChromium;
    CEFWindowParent1: TCEFWindowParent;
    Button1: TsButton;
    Memo1: TsMemo;
    procedure FormShow(Sender: TObject);
    procedure sButton1Click(Sender: TObject);
    procedure Chromium1TextResultAvailable(Sender: TObject;
      const aText: string);
    procedure Chromium1LoadingStateChange(Sender: TObject;
      const browser: ICefBrowser; isLoading, canGoBack,
      canGoForward: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
Chromium1.CreateBrowser(CEFWindowParent1, '');
end;

procedure TForm1.sButton1Click(Sender: TObject);
begin
Chromium1.LoadURL('https://google.com');
end;

procedure TForm1.Chromium1LoadingStateChange(Sender: TObject;
  const browser: ICefBrowser; isLoading, canGoBack, canGoForward: Boolean);
begin
Chromium1.RetrieveHTML;
end;

procedure TForm1.Chromium1TextResultAvailable(Sender: TObject;
  const aText: string);
begin
Memo1.Text :=  aText;
end;


end.

Demo I reviewed everything. For my case, there is little information, and version incompatibility, although I tried to somehow optimize the code to run on my version. In the demo: Minibrowser there was something similar, but it is incompatible with my version.
User avatar
salvadordf
Posts: 3067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CEF4 Get HTML Source

Post by salvadordf »

Use the SimpleBrowser2 demo to have a working browser and add this code to the TChromium.OnLoadingStateChange event :

Code: Select all

if not isLoading then Chromium1.RetrieveHTML;
CEF triggers most of the events in a thread that it's not the main application thread. VCL is not thread safe and it's recommended to modify the VCL controls outside the TChromium events.

Some demos are oversimplified and they don't follow this recommendation but if you get weird errors then save the aText parameter from the TChromium.OnTextResultAvailable event and send a custom Windows message to the main form to update Memo1.Text in the main application thread.
extasy
Posts: 5
Joined: Mon Jul 05, 2021 6:19 pm
Location: UK
Contact:

Re: CEF4 Get HTML Source

Post by extasy »

Wow o_O It worked !!!I am very glad, I was already very upset because nothing is working out, and then everything worked out! I am very grateful to you. I will follow the examples, thank you very much, and for your huge contribution to the development. I really liked your project "BriskBard" Thank you very much, and sorry for asking the same question, probably already for many messages!!!

And tell me the last question, is there a CEF4 of receiving the page text (Not the HTML code). The standard Twebbrowser has it. Is there such a function here?
User avatar
salvadordf
Posts: 3067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CEF4 Get HTML Source

Post by salvadordf »

I'm glad I could help. :)

Use TChromium.RetrieveText the same way as TChromium.RetrieveHTML

See the MiniBrowser demo for all the details.
extasy
Posts: 5
Joined: Mon Jul 05, 2021 6:19 pm
Location: UK
Contact:

Re: CEF4 Get HTML Source

Post by extasy »

Good job!!! Everything turned out great!:)) Thank you so much for the hint, it helped a lot)))) There is one moment when compiling SimpleBrowser2_D7 error: "creating an output file". It is solved by removing the "Output Directory" in the project options (Project - Options - Directories - Output Directory). I immediately looked there what's the matter, and others may not remove it and think why they don't compile:))
User avatar
salvadordf
Posts: 3067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CEF4 Get HTML Source

Post by salvadordf »

When a demo crashes, the destruction steps are not correctly implemented or there's some other bug in the demo or in CEF4Delphi then you may see some orphan CEF processes in the task manager.

Those processes use the EXE generated by the compiler and Delphi can't build a new EXE because the old one is still being used.

Open the task manager and terminate those orphan processes. Then you'll be able to build the demo again.
extasy
Posts: 5
Joined: Mon Jul 05, 2021 6:19 pm
Location: UK
Contact:

Re: CEF4 Get HTML Source

Post by extasy »

Thank you very much! :)
Post Reply