Page 1 of 1

Retrieving text from a website

Posted: Wed Nov 08, 2017 10:41 pm
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

Re: Retrieving text from a website

Posted: Thu Nov 09, 2017 8:02 am
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.

Re: Retrieving text from a website

Posted: Thu Nov 09, 2017 9:48 am
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.

Re: Retrieving text from a website

Posted: Thu Nov 09, 2017 11:26 pm
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