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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Retrieving text from a website

Post Reply
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

Retrieving text from a website

Post by PioPio »

Hello,

I am developing an application that pulls some data from a DB. Based on this data it runs a search on Google and then stores the text of the page I get from Google in a string.

I have the following classes:

Code: Select all

ISourceContainer = interface(ICefStringVisitor)
  function Source: ustring;
end;

TSourceContainer = class(TCefStringVisitorOwn, ISourceContainer)
private
  FSource: string;
protected
  procedure Visit(const str: ustring); override;
public
  function Source: ustring;
end;

TMainForm = class(TForm)
  Procedure TMainForm.ChromiumLoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer);
  private
    { Private declarations }
    FSourceContainer: ISourceContainer;
end;
and this is the relevant implementation

Code: Select all

procedure TMainForm.ChromiumLoadEnd(Sender: TObject; const browser: ICefBrowser;
  const frame: ICefFrame; httpStatusCode: Integer);
var s:string;
begin
  if frame = nil then
    exit;

  if httpStatusCode = 200 then
  begin
    frame.GetText(FSourceContainer);//text is retrieved here
    s:=FSourceContainer.Source;//text is stored in a string
  end;
end;


{ TSourceContainer }

function TSourceContainer.Source: ustring;
begin
  Result := FSource;
end;

procedure TSourceContainer.Visit(const str: ustring);
begin
  FSource := str;
end;
The text of the page is retrieved only if I fire ChromiumLoadEnd twice.

Is there a way to get the text of the webpage without firing ChromiumLoadEnd twice? What is the best approach in these cases ?


Many thanks
Alberto
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Retrieving text from a website

Post by salvadordf »

Hi,

The ICefStringVisitor.Visit function is called asynchronously. The FSourceContainer.Source property doesn't have any data if you read it right after calling frame.GetText(FSourceContainer).

I'll add some functions in TChromium to read all the text.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Retrieving text from a website

Post by salvadordf »

Please, download CEF4Delphi again.

Now you only have to call TChromium.RetrieveText and you will receive the text in the TChromium.OnTextResultAvailable event.
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

Re: Retrieving text from a website

Post by PioPio »

Wow ! I wasn't expecting that!

Thank you very much for adding the method. This is very handy and works perfectly ;)

Many thanks
Alberto
Post Reply